How To Do Amazingly Simple Searches With NSArray & NSPredicate

Matthew Campbell, March 25th, 2010

Cocoa-Touch ships with an super-useful class that will help you search through your lists of objects just like they were coming from a database. The class is called NSPredicate which also works with Core Data. This is very powerful stuff, especially what you are working with data intensive applications.

First off, keep in mind that generally we are using NSArray to manage lists of objects in our apps. The objects that NSArray stores for us are made up of properties like name, length and whatever is required for the objects in question. NSPredicate works by defining queries that select objects from an array based on the properties of the objects in the array.

Here are the steps required to search arrays with NSPredicate:

Essentially, these three steps are all it takes to filter an array. Of course, you will need some other pieces in place to do some meaningful work. Next, I will show you a concrete example of using NSPredicate with a custom class.

The first thing I did for this example was to create a custom class called RowOfData that could correspond to an individual row of data in a database.

NOTE: this does not mean that you really use SQL per-say with NSPredicate – the types of queries are more like a regex style. Check this Apple documentation for more details on what types of queries are available to you.

Anyway, here is the class definition for RowOfData:

Each RowOfData object in the array we are about to create represents an individual and details about his or her job and salary.

In effect, we are creating a list of objects that is a bit like a database table. Note here that listOfItems is declared as an NSMutableArray at the top of the file where this code would be located since it needs to stay in scope though-out the lifecycle of objects made from the class:

To summarize, we create an array and then create each object and add each object to the array. Here is what the results would look like if we were to present this array in a table view:

default_button.png

So here is what you have been waiting for. To display a subset of the dataset you would need to code an NSPredicate using a query string. For instance, if we wanted to find out who in our dataset were programmers we could do something like this:

Pretty simple huh? All you need to do is create the NSPredicate with our query and send the filterUsingPredicate message to your array using the NSPredicate object as an argument. If we tack this query to the end of our code and again run an app using a table view it would look like this:

default_button.png

Remember that we can do this because “job” is a property of the objects contain in the array. You can query based on other properties as well. Here are some examples of what you can do:

Find out who makes at least $100,000 per year:

Find everyone who has a name that begins with “T”:

I could throw examples at you all day but ultimately this will be useful when you can apply it the objects you use in your own apps. Check this Apple documentation for a complete list of the functions you can use with NSPredicate.

Another nice touch is that this method may be used with classes other than NSArray. One important use of NSPredicate is with Core Data. You can actually use NSPredicate to query the underlying data store managed by Core Data. This gives you a lot of powerful and efficient data management techniques right out of the box.