Filtering

When you filter a collection, you compare its objects to a logical statement to get a resultant collection that only contains objects for which the statement is true.

A predicate contains a statement that might be true, like The employeeID is greater than 75. There is a class called NSPredicate. NSMutableArray has a handy method for discarding all the objects that do not satisfy the predicate:

-​ ​(​v​o​i​d​)​f​i​l​t​e​r​U​s​i​n​g​P​r​e​d​i​c​a​t​e​:​(​N​S​P​r​e​d​i​c​a​t​e​ ​*​)​p​r​e​d​i​c​a​t​e​;​

With NSArray, you cannot remove objects that do not match the predicate. Instead, NSArray has a method that creates a new array that contains all the objects that satisfy the predicate:

-​ ​(​N​S​A​r​r​a​y​ ​*​)​ ...

Get Objective-C Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.