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

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -