func findKeyForValue(value: String, dictionary: [String: [String]]) ->String? { for (key, array) in dictionary { if (array.contains(value)) { return key } } return nil }
Call the above function that will return an optional string?
let drinks = ["Soft Drinks": ["Cocoa-Cola", "Mountain Dew", "Sprite"], "Juice" :["Orange", "Apple", "Grape"]] print(self.findKeyForValue("Orange", dictionary: drinks))
This function returns only the first key of the array that has the passed value.
abintom
source share