< Previous PageNext Page > Hide TOC

Predicates Overview

This article provides an overview of predicates and expressions, and how you represent them in Cocoa.

Contents:

Basics
Predicate Classes
Constraints and Limitations


Basics

A predicate is a logical operator that returns a Boolean value (true or false). There are two types of predicate; comparison predicates, and compound predicates:

Cocoa supports a wide range of types of predicate, including the following:

You can also create predicates for relationships—such as group.name matches 'work.*', ALL children.age > 12, and ANY children.age > 12—and for operations such as @sum.items.price < 1000.

Cocoa predicates provide a means of encoding queries in a manner that is independent of the store used to hold the data being searched. You use predicates to represent logical conditions used for constraining the set of objects retrieved by Spotlight and Core Data, and for in-memory filtering of objects.

You can use predicates with any class of object, but note that a class must be key-value coding compliant for the keys you want to use in a predicate (see Key-Value Coding Programming Guide).

Predicate Classes

Cocoa provides three predicate classes: NSPredicate, and two subclasses of NSPredicate, NSComparisonPredicate and NSCompoundPredicate.

NSPredicate provides—amongst others—methods to evaluate a predicate, and to create a predicate from a string (such as firstName like 'Mark'). In many cases you simply use NSPredicate. When you create a predicate using a string, NSPredicate creates the appropriate predicate and expression instances for you. In some situations, you want to create comparison or compound predicates yourself, in which case you can use NSComparisonPredicate and NSCompoundPredicate directly.

Predicate expressions in Cocoa are represented by instances of NSExpression. The simplest expression simply represents a constant value. Frequently, though, you use expressions that retrieve the value for a key path of the object currently being evaluated in the predicate. You can also create an expression to represent the object currently being evaluated in the predicate, to serve as a placeholder for a variable, or to return the result of performing an operation on an array.

How you create predicates and expressions is discussed in more detail in “Creating Predicates.”

Constraints and Limitations

If you use predicates with Core Data or Spotlight, you should take care that they will work with the data store. There is no specific implementation language for predicate queries—a predicate query may be translated into SQL, XML, or another format, depending on the requirements of the backing store (if indeed there is one).

The predicate system is intended to support a useful range of operators, so provides neither the set union nor the set intersection of all operators supported by all backing stores. Therefore, not all possible predicate queries are supported by all backing stores, and not all operations supported by all backing stores can be expressed with NSPredicate and NSExpression. The back end may downgrade the predicate (for example it may make a case-sensitive comparison case-insensitive) or raise an exception if you try to use an unsupported operator. For example:



< Previous PageNext Page > Hide TOC


© 2005, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-05-06)


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.