Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.4 and later. |
Companion guide | |
Declared in | NSPredicate.h |
Related sample code |
The NSPredicate
class is used to define logical conditions used to constrain a search either for a fetch or for in-memory filtering.
You use predicates to represent logical conditions, used for describing objects in persistent stores and in-memory filtering of objects. Although it is common to create predicates directly from instances of NSComparisonPredicate
, NSCompoundPredicate
, and NSExpression
, you often create predicates from a format string which is parsed by the class methods on NSPredicate
. Examples of predicate format strings include:
Simple comparisons, such as grade == "7"
or firstName like "Shaffiq"
Case/diacritic insensitive lookups, such as name contains[cd] "itroen"
Logical operations, such as (firstName like "Mark") OR (lastName like "Adderley")
With Mac OS X version 10.5 and later, you can create “between” predicates such as date between {$YESTERDAY, $TOMORROW}
.
You can create predicates for relationships, such as:
group.name like "work*"
ALL children.age > 12
ANY children.age > 12
You can create predicates for operations, such as @sum.items.price < 1000
. For a complete syntax reference, refer to the Predicate Programming Guide.
You can also create predicates that include variables, so that the predicate can be pre-defined before substituting concrete values at runtime. On Mac OS X v10.4, for predicates that use variables, evaluation is a two step process (see predicateWithSubstitutionVariables:
and evaluateWithObject:
). Mac OS X v10.5 introduces a new method, evaluateWithObject:substitutionVariables:
, which combines these steps.
+ predicateWithFormat:
+ predicateWithFormat:argumentArray:
+ predicateWithFormat:arguments:
– predicateWithSubstitutionVariables:
+ predicateWithValue:
Creates and returns a new predicate formed by creating a new string with a given format and parsing the result.
+ (NSPredicate *)predicateWithFormat:(NSString *)format, ...
The format string for the new predicate.
A comma-separated list of arguments to substitute into format.
A new predicate formed by creating a new string with format and parsing the result.
For details of the format of the format string and of limitations on variable substitution, see Predicate Format String Syntax.
NSPredicate.h
Creates and returns a new predicate by substituting the values in a given array into a format string and parsing the result.
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(NSArray *)arguments
The format string for the new predicate.
The arguments to substitute into predicateFormat. Values are substituted into predicateFormat in the order they appear in the array.
A new predicate by substituting the values in arguments into predicateFormat, and parsing the result.
For details of the format of the format string and of limitations on variable substitution, see Predicate Format String Syntax.
NSPredicate.h
Creates and returns a new predicate by substituting the values in an argument list into a format string and parsing the result.
+ (NSPredicate *)predicateWithFormat:(NSString *)format arguments:(va_list)argList
The format string for the new predicate.
The arguments to substitute into predicateFormat. Values are substituted into predicateFormat in the order they appear in the argument list.
A new predicate by substituting the values in argList into predicateFormat and parsing the result.
For details of the format of the format string and of limitations on variable substitution, see Predicate Format String Syntax.
NSPredicate.h
Creates and returns a predicate that always evaluates to a given value.
+ (NSPredicate *)predicateWithValue:(BOOL)value
The value to which the new predicate should evaluate.
A predicate that always evaluates to value.
NSPredicate.h
Returns a Boolean value that indicates whether a given object matches the conditions specified by the receiver.
- (BOOL)evaluateWithObject:(id)object
The object against which to evaluate the receiver.
YES
if object matches the conditions specified by the receiver, otherwise NO
.
NSPredicate.h
Returns a Boolean value that indicates whether a given object matches the conditions specified by the receiver after substituting in the values in a given variables dictionary.
- (BOOL)evaluateWithObject:(id)object substitutionVariables:(NSDictionary *)variables
The object against which to evaluate the receiver.
The substitution variables dictionary. The dictionary must contain key-value pairs for all variables in the receiver.
YES
if object matches the conditions specified by the receiver after substituting in the values in variables for any replacement tokens, otherwise NO
.
This method returns the same result as the two step process of first invoking predicateWithSubstitutionVariables:
on the receiver and then invoking evaluateWithObject:
on the returned predicate. This method is optimized for situations which require repeatedly evaluating a predicate with substitution variables with different variable substitutions.
NSPredicate.h
Returns the receiver’s format string.
- (NSString *)predicateFormat
The receiver’s format string.
NSPredicate.h
Returns a copy of the receiver with the receiver’s variables substituted by values specified in a given substitution variables dictionary.
- (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)variables
The substitution variables dictionary. The dictionary must contain key-value pairs for all variables in the receiver.
A copy of the receiver with the receiver’s variables substituted by values specified in variables.
The receiver itself is not modified by this method, so you can reuse it for any number of substitutions.
NSPredicate.h
© 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-08-16)