how to get key from respective value in a NSDictionary?
I am aware of allKeys function but do we have a method which returns a single key for a single value.
how to get key from respective value in a NSDictionary? I am aware of
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well because a value can be in a NSDictionary multiple times, there is no way to just say “Give me the key for this value”. But you CAN say “Give me all keys containing this value”.
NSArray* arrayOfKeys = [yourDictionary allKeysForObject:myObject];If the value is only once in the dictionary, you can just extract it, using:
YourObject* o = [arrayOfKeys firstObject];But, always do a NIL and count check on this array. Out of bounds exceptions ahead!
P.S. Credits to @Hagile for
firstObjectmethod instead ofobjectAtIndex:0