Cocoa predicates provide a means of encoding queries in a manner independent of the backing 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.
There are two types of predicate, comparison and compound.
Comparison predicates are used to compare two expressions. Comparison predicates have a left expression, a right expression, and an operator, and return the result of invoking the operator with the results of evaluating the expressions.
Compound predicates join two or more predicates using a logical operator (AND or OR), or negate another predicate (a NOT operation).
Comparison predicates and compound predicates are represented in Cocoa by instances of NSComparisonPredicate and NSCompoundPredicate respectively. Both classes inherit from NSPredicate.
Cocoa supports a wide range of types of predicate, including the following:
Simple comparisons, such as grade == 7 or firstName like 'Mark'
Case or diacritic insensitive lookups, such as name contains[cd] 'citroen'
Logical operations, such as (firstName beginswith 'M') AND (lastName like 'Adderley')
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.
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.
Predicate expressions in Cocoa are represented by instances of NSExpression. The simplest expression simply represents a constant value, as shown in the following example.
NSExpression *expression = [NSExpression |
expressionForConstantValue:[NSDecimalNumber numberWithInt:10]]; |
You frequently use expressions that retrieve the value for a key path of the object currently being evaluated in the predicate.
NSExpression *expression = [NSExpression expressionForKeyPath:@"lastName"]; |
You can also create an expression to represent the object currently being evaluated in the predicate, to serve as a placeholder for a variable to be extracted from a variable bindings dictionary, or to return the result of performing an operation on an array, as illustrated by the following example.
NSExpression *expression = [NSExpression expressionForFunction:@"max" arguments:anArray]; |
The expression system is currently not extensible.
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:
Para
The ANYKEY operator can only be used with Spotlight.
Spotlight does not support relationships.
© 2005, 2009 Apple Inc. All Rights Reserved. (Last updated: 2009-11-17)