I am very new to objective-C and came across the NSDictionary method allKeysForObject:. Seems very useful. However, I have a NSDictionary which has several NSArrays (all of length 2) and which are keyed by NSStrings. Basically, the keys are items and the arrays define their two properties. If I wanted to pull all the item names that have a certain property, could this be done with something like allKeysForObject, or should I just loop over the dictionary and grow a mutable array (seems inefficient).
I’d include a code snippet, but I feel like this question is conceptual enough that code wouldn’t really clarify anything. Oh, what the hell. Here’s some simplified code:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:2],nil],@"Car",[NSArray arrayWithObjects:[NSNumber numberWithInt:2],[NSNumber numberWithInt:3],nil],@"Boat",nil];
NSLog(@"%@",[dict allKeysForObject:???]); // this is the line I am not at all sure about.
EDIT: Thank you for the responses so far. I was not clear about my question, though. I am looking for a way to do something more general. I don’t want to retrieve all keys for a particular object, say [1,2], but I want to look in the dictionary for all arrays including the NSNumber 1 and return those keys. So if I added @"Plane",[NSArray arrayWithObjects:[NSNumber numberWithInt:1],[NSNumber numberWithInt:3],nil], I’d like to somehow query for the NSNumber 1 and get @"Car" and @"Plane". I am getting the sense that this is not what this method was designed to do.
You are looking for
-keysOfEntriesPassingTest:…