There are a number of steps for creating and using the Description expression.
First you need to create expressions ( NSExpression
instances) to represent the key path for the value you are interested in and represent the function you want to apply (for example, max: or min :):
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"salary"]; NSExpression *maxSalaryExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
For a complete list of supported functions, see expressionForFunction:arguments:
Then you create a description of the expression and specify its name, expression and type of result.
The name is the key that will be used in the dictionary for the return value. If you want to get multiple values ββ- for example, the largest and lowest salaries in the Employee table - the name of each description description must be unique for a given query query.
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName:@"maxSalary"]; [expressionDescription setExpression:maxSalaryExpression]; [expressionDescription setExpressionResultType:NSDecimalAttributeType];
Finally, you set query properties to retrieve only the property represented by the expression:
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]