NSDictionary Key For Value/Object?
Solution 1
-[NSDictionary allKeysForObject:]
returns an NSArray
of all the keys whose objects match the passed object, where "match" is determined by isEqual:
.
Solution 2
To answer you question in a more specific manner, use the following to get a key for a particular object:
NSString *knownObject = @"the object";
NSArray *temp = [dict allKeysForObject:knownObject];
NSString *key = [temp lastObject];
//"key" is now equal to the key of the object you were looking for
Solution 3
This is how you can get a first key for object (in case it is an NSString
):
NSArray *keys = [yourDic allKeysForObject:yourObject];
NSString *yourKey;
if ([keys count] > 0) {
yourKey = keys[0];
}
This handles if the object is not found in in the dictionary.
Related videos on Youtube
Syed Absar
Syed Absar is an enthusiastic technology specialist in mobility with a passion for professional perfection, eager to learn and continuously improve. My strength includes expertise in programming combined with strong system and business analysis skills, agile evangelism, collaboration & demonstrations throughout the entire project lifecycle. He has programmed point of sales, workflow systems over the web, networked desktop games, mobile engineering applications and a lot more. Absar has been a professional development (Java, C#, PHP, C, Python, J2ME, iPhone, Android, ASP.NET etc) trainer for many years and has spoken at multiple seminars and code camps. He has also contributed to a recent research on semantic web ontology in Malaysia & Developed an extension for Helpdesk system in the United States. Absar holds a bachelors degree in software engineering from University of Karachi, moreover, he is certified as a technology specialist by Microsoft, internet specialist by Juniper Networks, and another certified IT professional title from the government of Pakistan. He had exceptional performance during the academics as well which reflects in his professional level academic projects like network based quiz engine based on sockets programming and high level business logics, artificial intelligence based pharmacy consultant, etc.
Updated on July 05, 2022Comments
-
Syed Absar almost 2 years
Can we get the key for an object in an
NSDictionary
by passing a particular value or object? -
Kai Huppmann almost 13 yearsbut remember: it gives all keys for objects matching 'isEqual' not '=='
-
jscs almost 13 years@Kai: True. I should perhaps also add a note about it returning an
NSArray
although that should be clear from the method name. -
jscs almost 13 yearsIf this isn't an FGITW question and answer, I don't know what is. Five upvotes just because I have a search shortcut in my browser.
-
AddisDev over 10 yearsIn the case the object is not a string, replace NSString with the object class. This is self explanatory.
-
Max Steinmeyer over 10 yearsYou might prefer
lastObject
toobjectAtIndex:0
to avoid throwing an exception if there are no such keys. -
Abizern about 10 yearsOr even
firstObject
if you are using the latest SDK