ios - Swift Dictionary Get Key for Values -


i have dictionary defined as:

let drinks = [string:[string]]()  drinks = ["soft drinks": ["cocoa-cola", "mountain dew", "sprite"],  "juice" :["orange", "apple", "grape"]] 

how can key, given value.

let key = (drinks nsdictionary).allkeysforobject("orange") as! string print(key) //returns empty array. should return "juice" 

func findkeyforvalue(value: string, dictionary: [string: [string]]) ->string? {     (key, array) in dictionary     {         if (array.contains(value))         {             return key         }     }      return nil } 

call above function return optional string?

let drinks = ["soft drinks": ["cocoa-cola", "mountain dew", "sprite"],         "juice" :["orange", "apple", "grape"]]  print(self.findkeyforvalue("orange", dictionary: drinks)) 

this function return first key of array has value passed.


Comments

Popular posts from this blog

xml - Extract substrings with XSLT -

math - "ELO Rating" and how does "TSR Rating" work? -

How can do a tuple comparison in coffeescript similar to that in python? -