iOS a dictionary where multiple keys are the same

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
161
4
I'm trying to devise a method to deal with entries in a Dictionary with multiple keys values that are the same (though different values). How is that handled programatically? Does Swift provide any means to do that? - or is the solution to make multiple Dictionaries and place the keys that are same in different dictionaries?


One idea to solve this that I tried before was to make the values into keys and the keys into values. However because of the nature of the lists I'm planning to create this won't work for this particular situation. Any ideas?

thanks for your suggestions.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
You cannot have a dictionary where multiple keys are the same. I think you need to give a better description of the problem so we can propose solutions.

Anyway, sounds like an array of structs.
 

theprizerevealed

macrumors regular
Original poster
Feb 26, 2016
161
4
Well, to generalize let's say I have some data such as this:

"calico cat"
"BLACK spots brown spots white spots cat"
"brown spots BLACK spots white spots cat"
"brown spots white spots BLACK spots cat"

"calico cat" = "BLACK spots brown spots white spots cat" = "brown spots BLACK spots white spots cat" = "brown spots white spots BLACK spots cat"

I want to represent in the Dictionary thusly:

"calico cat" = "BLACK spots brown spots white spots cat"
"calico cat" = "brown spots BLACK spots white spots cat"
"calico cat" = "brown spots white spots BLACK spots cat"

"BLACK spots brown spots white spots cat" = "brown spots BLACK spots white spots cat"
"BLACK spots brown spots white spots cat" = "brown spots white spots BLACK spots cat"
"BLACK spots brown spots white spots cat" = "calico cat"

"brown spots BLACK spots white spots cat" = "BLACK spots brown spots white spots cat"
"brown spots BLACK spots white spots cat" = "brown spots white spots BLACK spots cat"
"brown spots BLACK spots white spots cat" = "calico cat"

"brown spots white spots BLACK spots cat" = "BLACK spots brown spots white spots cat"
"brown spots white spots BLACK spots cat" = "brown spots BLACK spots white spots cat"
"brown spots white spots BLACK spots cat" = "calico cat"





hope that's not too confusing.....I can capitalize some of the colors if you need it to help illustrate clearly what I'm trying to say...Unfortunately because there are four or five values associated with each key it seems I need a new method than the one I used before.
 

dantastic

macrumors 6502a
Jan 21, 2011
569
674
Each key in a dictionary must be unique, otherwise you couldn't tell them apart you know.
Not saying this is the best way of solving your problem as looking at your data but here we go anyway. Let each key reference an array in the dictionary. Like so,
Code:
"calico cat" = ["BLACK spots brown spots white spots cat", "brown spots BLACK spots white spots cat", "brown spots white spots BLACK spots cat"]
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
You gave more of a description of your solution, not of your problem.

struct Cat {
let kind: String // "calico cat"
let properties: String // "BLACK spots brown spots white spots cat"
}

let cats: [Cat]

let calicoCats = cats.filter { $0.kind == "calico cat" }
let catKinds = cats.map { $0.kind }.uniqued()
let catsWithMatchingProperties = cats.filter { $0.properties == "BLACK spots brown spots white spots cat" }

I don't really understand what you're doing with the data or why you want it to be in a dictionary. The array of structs is flexible and allows you to represent all of your different cats and search for them based on kind or properties. You might also use multiple dictionaries.

If this doesn't help you need to explain the problem some more.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.