a dictionary where multiple keys are the same

Discussion in 'iOS Programming' started by theprizerevealed, Oct 6, 2018.

  1. theprizerevealed macrumors member

    Joined:
    Feb 26, 2016
    #1
    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.
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Sounds like a Dictionary of Arrays instead of single values
     
  3. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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.
     
  4. theprizerevealed thread starter macrumors member

    Joined:
    Feb 26, 2016
    #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.
     
  5. dantastic macrumors 6502a

    dantastic

    Joined:
    Jan 21, 2011
    #5
    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"]
     
  6. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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.
     

Share This Page