您可以使用以下获取请求:
// Fetch request for your entity:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Entity"];
[request setResultType:NSDictionaryResultType];
// Restrict result to "level == 5":
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"level == %d", 5];
[request setPredicate:predicate];
// Expression description for "@sum.value":
NSExpression *sumExpression = [NSExpression expressionForKeyPath:@"@sum.value"];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"sumValue"];
[expressionDescription setExpression:sumExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];
[request setPropertiesToFetch:@[expressionDescription]];
NSArray *result = [context executeFetchRequest:request error:&error];
您的数据的结果是
(
{
总和 = 94;
}
)
即一个数组,其中包含一个字典,其值的总和为 level=5。