NSMutableArray - request elements without enumeration?

Using LINQ in .Net I can select elements from an array that meet certain criteria, for example, from an array of People:

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

Is there anything similar for NSMutableArray? I have a lot of elements in it, and listing it with a loop is a pretty expensive execution.

+7
objective-c cocoa
source share
4 answers
+7
source share

I created a simple library called Linq to ObjectiveC , which is a set of methods that provide a Linq-style query interface. In your case, you need the Linq-to-ObjectiveC method where :

 NSArray* peopleWhoAreSmart = [people where:^BOOL(id person) { return [[person iq] intValue] > 110; }]; 

This returns an array of people, where their IQ> 110.

+4
source share

Another option is to use Higher Order Messages to implement the selection. For example,

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

Where more IQ is a category method for people.

0
source share

Of course, these (10.6+) days we have good APIs like indexOfObjectPassingTest to do things like

 var smartPeople = [People indexesOfObjectsPassingTest:^BOOL(id person, NSUInteger idx, BOOL *stop) { return person.iq > 110; } ]; 
0
source share

All Articles