Contents

NSExpression

An expression for use in a comparison predicate.

Declaration

class NSExpression

Overview

Comparison operations in an NSPredicate derive from two expressions as instances of the NSExpression class. You create expressions for constant values, key paths, and so on.

Generally, anywhere in the NSExpression class hierarchy where there’s a composite API and subtypes that may only reasonably respond to a subset of that API, invoking a method that doesn’t make sense for that subtype throws an exception.

Aggregate Expressions

NSExpression.ExpressionType.aggregate allows you to create predicates containing expressions that evaluate to collections that contain further expressions. The collection may be an NSArray, NSSet, or NSDictionary object.

Core Data doesn’t support aggregate expressions.

Subquery Expressions

The NSExpression.ExpressionType.subquery creates a subexpression that returns a subset of a collection of objects. This allows you to create sophisticated queries across relationships, such as a search for multiple correlated values on the destination object of a relationship.

Set Expressions

The set expressions (NSExpression.ExpressionType.unionSet, NSExpression.ExpressionType.intersectSet, and NSExpression.ExpressionType.minusSet) combine results in a manner similar to the NSSet methods.

Both sides of these expressions must evaluate to a collection; the left side must evaluate to an NSSet object, and the right side can be any other collection type.

(expression UNION expression)
(expression INTERSECT expression)
(expression MINUS expression)

Core Data doesn’t support set expressions.

Function Expressions

In macOS 10.4, NSExpression only supports a predefined set of functions: sum, count, min, max, and average. You access these predefined functions in the predicate syntax using custom keywords (for example, MAX(1, 5, 10)).

In macOS 10.5 and later, function expressions also support arbitrary method invocations. To implement this extended functionality, use the syntax FUNCTION(receiver, selectorName, arguments, ...), as in the following example:

FUNCTION(@"/Developer/Tools/otest", @"lastPathComponent") => @"otest"

All methods must take one or more id arguments and return an id value, although you can use the CAST expression to convert datatypes with lossy string representations (for example, CAST(####, "NSDate")). macOS 10.5 extends the CAST expression to provide support for casting to classes for use in creating receivers for function expressions.

Although Core Data supports evaluation of the predefined functions, it doesn’t support the evaluation of custom predicate functions in the persistent stores (during a fetch).

Topics

Creating an Expression

Creating an Expression for a Value

Creating a Collection Expression

Creating a Subquery

Creating a Conditional Expression

Creating an Expression Using Blocks

Creating an Expression for a Function

Getting Information About an Expression

Evaluating an Expression

Accessing the Expression Block

Initializers

See Also

Filltering