本文介绍了在谓词中使用@min的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前获取@min或@max值的版本是:

My current version for get @min or @max value is:

    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的数组.看起来这不是一种填充对象数组的好方法(内存和处理时间成本).也许有人可以帮助重构它?

allSpecific is array of NSManagedObjects.Looks like is a not good way to fill array of objects before (memory and process time cost). Probably somebody can help refactor it?

推荐答案

因此,您有一个 NSManagedObjects 的数组( allSpecifics ),并且这些对象都有一个属性?如果正确,那么:

So you have an array (allSpecifics) of NSManagedObjects, and these objects have a rate property? If that's correct then:

要查找:

  • 最低费率值:
    NSNumber * minimumRate = [allSpecifics valueForKeyPath:@"@ min.rate"];
  • 最大速率值:
    NSNumber * minimumRate = [allSpecifics valueForKeyPath:@"@ max.rate"];
  • 平均费率值:
    NSNumber * minimumRate = [allSpecifics valueForKeyPath:@"@ avg.rate"];
  • 总费率值:
    NSNumber * minimumRate = [allSpecifics valueForKeyPath:@"@ sum.rate"];

这篇关于在谓词中使用@min的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 22:11