user2924482 user2924482 -4 years ago 154
Swift Question

Swift : Query Dictionary using value

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.


//output: 100

//but I want to get:
//output: key

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

Answer Source

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"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download