Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluating an NSPredicate on a NSArray (without filtering)

Is it possible to evaluate a NSPredicate on a NSArray, without having the NSPredicate starting to filter out objects in the array?

For instance, say I have the following predicate that just checks the number of objects in an array:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"count == 3"];
NSArray *list = [NSArray arrayWithObjects:@"uno", @"dos", @"volver", nil];
BOOL match = [pred evaluateWithObject:list];

This will crash, as pred will try to retrieve the "count" key from the first object in the array instead of the array itself.

like image 647
cfischer Avatar asked Oct 18 '11 10:10

cfischer


1 Answers

Use the SIZE operator of NSPredicate which is equivalent to count method of NSArray.

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF[SIZE] == 3"];
NSArray *list = [NSArray arrayWithObjects:@"uno", @"dos", @"volver", nil];
BOOL match = [pred evaluateWithObject:list];
like image 120
EmptyStack Avatar answered Oct 16 '22 00:10

EmptyStack