views:

177

answers:

2

Using LINQ in .Net I can select items from an array that match a particular criteria i.e. from an array called People:

var cleverPeople = People.Where(o=>o.IQ>110);

Is there anything similar I can do to an NSMutableArray? I have many items in it and enumerating it with a loop is pretty costly performance wise.

+6  A: 

See -[NSArray filteredArrayUsingPredicate:].

Chuck
beautiful. exactly what I needed. NSPredicate *pred = [NSPredicate predicateWithFormat:@"isFired == NO"]; NSArray *filtered = [self.arenamap filteredArrayUsingPredicate:pred];
jdee
Here's a nice write-up on NSPredicate vs LINQ.http://www.cimgf.com/2008/08/24/cocoa-tutorial-c-linq-or-cocoa-key-paths-and-nspredicate/
Lounges
Note that this will probably not be much faster than iterating the loop yourself. Fundamentally to select items from an array like this, the whole array must be iterated over somewhere along the line
Mike Abdullah
A: 

Another option would be to use Higher Order Messaging to implement select. For example,

NSArray* cleverPeople = [[People select] greaterIQ:110];

Where greaterIQ would be a category method on People.

Peter N Lewis