Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the key for a given object from an NSMutableDictionary?

I have an object which is in an big NSMutableDictionary, and need to find out which key this has. So I want to look up that "table" from both columns. Not only with keys, but also with objects (to get keys). Is that possible?

like image 559
dontWatchMyProfile Avatar asked May 07 '10 10:05

dontWatchMyProfile


3 Answers

Look to the parent class (NSDictionary)

- (NSArray *)allKeysForObject:(id)anObject

Which will return a NSArray of all the keys for a given Object Value. BUT it does this by sending an isEqual message to each object of the Dictionary so for your large dataset this may not be best performance.

Maybe you need to hold some form of additional indexing structure structure(s) to allow you to locate the objects on some critical values within them, linked to the key without direct object comparison

like image 119
Kevin Avatar answered Nov 05 '22 09:11

Kevin


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 objectAtIndex:0];

//"key" is now equal to the key of the object you were looking for
like image 30
AddisDev Avatar answered Nov 05 '22 10:11

AddisDev


Take a look at:

- (NSArray *)allKeysForObject:(id)anObject
like image 6
Tom Irving Avatar answered Nov 05 '22 09:11

Tom Irving