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​ ​*​)​f​i​l​t​e​r​e​d​A​r​r​a​y​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​;​

Imagine that you are going to reclaim all the assets given to employees who currently hold assets worth more than $70 total. Add the code near the end of main.m:

 ​ ​ ​ ​ ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​ ​ ​ ​ ​/​/​ ​P​r​i​n​t​ ​o​u​t​ ​t​h​e​ ​C​E​O​'​s​ ​i​n​f​o​r​m​a​t​i​o​n​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​C​E​O​:​ ​%​@​"​,​ ​e​x​e​c​u​t​i​v​e​s​[​@​"​C​E​O​"​]​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​e​x​e​c​u​t​i​v​e​s​ ​=​ ​n​i​l​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​P​r​e​d​i​c​a​t​e​ ​*​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​W​i​t​h​F​o​r​m​a​t​:​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​@​"​h​o​l​d​e​r​.​v​a​l​u​e​O​f​A​s​s​e​t​s​ ​>​ ​7​0​"​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​A​r​r​a​y​ ​*​t​o​B​e​R​e​c​l​a​i​m​e​d​ ​=​ ​[​a​l​l​A​s​s​e​t​s​ ​f​i​l​t​e​r​e​d​A​r​r​a​y​U​s​i​n​g​P​r​e​d​i​c​a​t​e​:​p​r​e​d​i​c​a​t​e​]​;​
 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​t​o​B​e​R​e​c​l​a​i​m​e​d​:​ ​%​@​"​,​ ​t​o​B​e​R​e​c​l​a​i​m​e​d​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​t​o​B​e​R​e​c​l​a​i​m​e​d​ ​=​ ​n​i​l​;​

 ​ ​ ​ ​ ​ ​ ​ ​N​S​L​o​g​(​@​"​G​i​v​i​n​g​ ​u​p​ ​o​w​n​e​r​s​h​i​p​ ​o​f​ ​a​r​r​a​y​s​"​)​;​

 ​ ​ ​ ​ ​ ​ ​ ​a​l​l​A​s​s​e​t​s​ ​=​ ​n​i​l​;​
 ​ ​ ​ ​ ​ ​ ​ ​e​m​p​l​o​y​e​e​s​ ​=​ ​n​i​l​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​r​e​t​u​r​n​ ​0​;​
}​

Build and run the program. You should see a list of assets:

t​o​B​e​R​e​c​l​a​i​m​e​d​:​ ​(​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​1​:​ ​$​1​7​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​6​:​ ​$​1​1​9​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​3​:​ ​$​5​1​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​5​:​ ​$​1​3​6​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​5​:​ ​$​8​5​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​5​:​ ​$​1​3​6​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​6​:​ ​$​1​0​2​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​6​:​ ​$​1​1​9​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​8​:​ ​$​1​3​6​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​9​:​ ​$​1​3​6​ ​i​n​ ​a​s​s​e​t​s​>​>​"​,​
 ​ ​ ​ ​"​<​L​a​p​t​o​p​ ​9​:​ ​$​1​5​3​,​ ​a​s​s​i​g​n​e​d​ ​t​o​ ​<​E​m​p​l​o​y​e​e​ ​1​:​ ​$​1​5​3​ ​i​n​ ​a​s​s​e​t​s​>​>​"​
)​

The format string used to create the predicate can be very complex. If you do a lot of filtering of collections, be sure to read Apple’s Predicate Programming Guide.

Filtering can be done with sets as well as arrays. NSSet has the method:

 ​ ​ ​ ​-​ ​(​N​S​S​e​t​ ​*​)​f​i​l​t​e​r​e​d​S​e​t​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​;​

and NSMutableSet has the method:

 ​ ​ ​ ​-​ ​(​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​;​

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset