Things like max, min, and avg can be calculated just fine,
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"]);
]
But, given I have "Item" entities, with the price I bought it for and the price I sold it for as attributes, how I calculate the total Profit of all Item entities using NSExpressions?
Thank you