我获取@min或@max值的当前版本是:
for (NSManagedObject *destination in allSpecifics)
{
[allRates addObject:[destination valueForKey:@"rate"]];
}
NSExpression *arrayExpression = [NSExpression expressionForConstantValue: allRates];
NSArray *argumentArray = [NSArray arrayWithObject: arrayExpression];
NSExpression* expression = [NSExpression expressionForFunction:@"min:" arguments:argumentArray];
id result = [expression expressionValueWithObject: nil context: nil];
NSNumber *rateForAdd = result;
allSpecific是NSManagedObjects的数组。 看起来像是一种填充对象数组的好方法(内存和处理时间成本)。可能有人可以帮助重构它吗?
答案 0 :(得分:5)
所以你有allSpecifics
的数组(NSManagedObjects
),这些对象有rate
属性?如果那是正确的话:
找到:
NSNumber *minimumRate = [allSpecifics valueForKeyPath:@"@min.rate"];
NSNumber *minimumRate = [allSpecifics valueForKeyPath:@"@max.rate"];
NSNumber *minimumRate = [allSpecifics valueForKeyPath:@"@avg.rate"];
NSNumber *minimumRate = [allSpecifics valueForKeyPath:@"@sum.rate"];