可以很好地计算诸如 max、min 和 avg 之类的东西,
NSExpression *maxDate = [NSExpression expressionForKeyPath:@"startDate"];
NSExpression *maxDateExpression = [NSExpression expressionForFunction:@"max:"
arguments:@[maxDate]];
NSExpressionDescription *maxDateExpressionDescription = [[NSExpressionDescription alloc] init];
[maxDateExpressionDescription setName:@"maxDate"];
[maxDateExpressionDescription setExpression:maxDateExpression];
[maxDateExpressionDescription setExpressionResultType:NSDateAttributeType];
[request setPropertiesToFetch:@[maxDateExpressionDescription]];
// Execute the fetch.
NSError *error = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (error) {
// Handle the error.
NSLog(@"Error!");
}
else {
NSLog(@"Max date is: %@", [objects[0] objectForKey:@"maxDate"]);
]
但是,鉴于我有“项目”实体,我购买它的价格和我出售它的价格作为属性,我如何使用 NSExpressions 计算所有项目实体的总利润?
谢谢