Reputation: 3565
I have NSDictionary
which contains following type of objects.
OBJECT1 - isFree:NO
Name: Papaya
OBJECT2 - isFree:YES
Name: Apple
OBJECT3 - isFree:YES
Name: Grapes
I can get object for the given key
by using objectForKey
as follows.
NSString* contentId = @"OBJECT3";
ACBContentType *object = [[[ACBLibrary sharedLibrary] contentsDictionary] objectForKey:contentId];
However I want to get all the objects which isFree:YES
by reading object isFree
property. What is the best way to do this?
Upvotes: 1
Views: 76
Reputation: 16650
If you are not interested in the keys, but solely the objects, it is pretty simply:
NSDictionary *contentDictionary = …;
NSPredicate *freePredicate = [NSPredicate predicateWithFormat:@"isFree == %@", @YES];
NSArray* freeContent = [[contentDictionary allValues] filteredArrayUsingPredicate:freePredicate];
Typed in Safari.
-allValues
returns an array of all value objects. -filteredArrayUsingPredicate:
reduces this to the items fulfilling the predicate.
Upvotes: 4