NSDictionary Key For Value/Object?

58,523

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.

Share:
58,523

Related videos on Youtube

Syed Absar
Author by

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, 2022

Comments

  • Syed Absar
    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
    Kai Huppmann almost 13 years
    but remember: it gives all keys for objects matching 'isEqual' not '=='
  • jscs
    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
    jscs almost 13 years
    If 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
    AddisDev over 10 years
    In the case the object is not a string, replace NSString with the object class. This is self explanatory.
  • Max Steinmeyer
    Max Steinmeyer over 10 years
    You might prefer lastObject to objectAtIndex:0 to avoid throwing an exception if there are no such keys.
  • Abizern
    Abizern about 10 years
    Or even firstObject if you are using the latest SDK