0

I have a dictionary but I need to query by particular value to get the key. But it seems like swift doesn't have valueforKey function.

Example:

print(myDictionary["key"])
//output: 100

//but I want to get:
print(myDictionary.valueForkey:"100")
//output: key

Any of you knows how can I do this in swift?

user2924482
  • 8,380
  • 23
  • 89
  • 173

1 Answers1

3

Of course you can do that in Swift because dictionaries can be represented by tuples:

let myDictionary = ["foo": 100, "baz" : 200]

if let found = myDictionary.filter({ $0.1 == 100 }).first {
    let key = found.0
    print(key) // "foo"
}
vadian
  • 274,689
  • 30
  • 353
  • 361