Introduction to NSPredicate

As I continued to work on my Rumford1797 app I made heavy usage of an Cocoa framework component called NSPredicate. Most of you that have come across NSPredicate have seen it in combination with CoreData when fetching objects from the persistent store. That was my first point of contact with it as well but lately I discovered that it is also useful for cleansing your code when working with any amount of data stored in structures like NSSet or NSArray.

WHAT IS NSPREDICATE?

NSPredicate is basically a predicate to filter objects. The advantage is, that it can be used to filter many collections of objects, e.g. NSSet or NSArray.

// create an array of (ns)strings
NSArray *looneyTunes = [NSArray arrayWithObjects:@"Bugs Bunny", @"Daffy Duck", @"Elmer Fudd", nil];
	
// create a predicate that looks for objects that begin with the character 'B'
NSPredicate *beginsWithB = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'B'"];
	
// this is where the magic happens
NSArray *looneyTunesThatBeginWithB = [looneyTunes filteredArrayUsingPredicate:beginsWithB];
	
// returns 'Results: 1'
NSLog(@"Results: %d", [looneyTunesThatBeginWithB count]);

EXAMPLES

As you can see, filtering an array by a simple predicate is just a single line of code. Even the most complex predicates work like this. So lets pretend we have an objectset of a class called LooneyTune that has the following structure:

Now we want to retrieve these objects from an array that have 'Bunny' as lastName.

NSArray *looneyTunes = [NSArray arrayWithObjects:bugsBunny,honeyBunny,lolaBunny,elmerFudd,nil];
NSPredicate *lastNameBunny = [NSPredicate predicateWithFormat:@"lastName like[c] 'Bunny'"];
NSArray *bunnies = [looneyTunes filteredArrayUsingPredicate:lastNameBunny];
	
// returns 'Results: 3'
NSLog(@"Results: %d", [bunnies count]);

That was again easy, wasn’t it? The next and last example will show you that it is also very easy to use more complex data types as strings like NSDate in you NSPredicates. Let’s say we want to find all LooneyTunes characters that had their first appearance before November 1st, 1966

NSDate *date1966 = [NSDate dateWithNaturalLanguageString:@"November 1, 1966"];
NSPredicate *appearanceBefore1966 = [NSPredicate predicateWithFormat:@"firstAppearance < %@",date1966];
NSArray *firstLooneyTunes = [looneyTunes filteredArrayUsingPredicate:appearanceBefore1966];
	
// returns 'Results: 2'
NSLog(@"Results: %d", [firstLooneyTunes count]);

ONE MORE THING ..

You might have guessed it but I felt the need to emphasize on this: You are able to combine multiple predicates with logical operators like AND and OR. A quick example will show you what I mean:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"(start >= %@) AND (end <= %@)", earlyDate, lateDate];

This simple line of code is able to extract objects (having the properties start and end) that are valid in a given period which is limited by earlyDate and lateDate. In my experience, this method is faster and much more reliable compared to a ‘for’ loop or similar classical approaches for this purpose.

For more information about NSPredicate and its usage please refer to the Predicates Programming Guide.

(The name LooneyTunes and the LooneyTunes characters which have been used for the code examples are property of Warner Bros.)

NIGHTNIGHT by DEDDY