简体   繁体   中英

How can i find a key name of value from the dictionary in swift?

i am trying to find a key name of particular value of dictionary without any kind of loop. I can get key name of "S" but when the "SE" i found then i am not able to find the key name of "SE". here is my Dictionary

dictionary {
    East = S;
    North =     (
        NE,
        E,
        SE
    );
    south =     (
        SW,
        W,
        NW
    );
    west = N;
}

Your dictionary is backwards. Convert it into the reverse of itself, so that S is a key and East is its value, NE is a key and North is its value, E is a key and North is its value, and so on. Now you have an instant lookup with no loop, as you say.

Assuming you're talking about a Dictionary Collection Type , you can only find a value based on it's key. not a key based on it's value. This is because multiple keys could have the same value. (and that's not the purpose of a dictionary to begin with). Your only option would be to iterate over the dictionary and find all the keys for the value you're using.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM