开发者

Combine key value functions with NSExpression

Can I combine key value collection functions with NSExpression? I was using it with CoreData to get the max: of a value from my managed object classes, and it was working great.

Now i have this other managed object class which contains an NSSet. I want to find the max: of the @sum of values in the NSSet.

So for example, I have 10 managed objects, each with an NSSet of values. I want to @sum the values in each set, and then find the largest one of the 10 managed objects.

I would usually get the sum using [object valueForKeyPath:@"usageMetrics.@sum.value"] - which works fine.

I want to combine and do somethinge like:

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"usageMetrics.@sum.value"];
NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];

It doesn't seem to be working

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath开发者_StackOverflow中文版 element (not a relationship or attribute): @sum'


I do this:

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"usageMetrics.value"]; 
NSExpression *valueSumExpression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];

which returns an NSArray of 12 results (this is how many managed objects it found) and it has summed the values.

I then do:

NSDecimalNumber *maxValue = [results valueForKeyPath:@"@max.maxValue"]; (maxValue being the name of the NSExpressionDescription)

which worked!

I would like to know if I could stack the sum: and the max: into an NSExpression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜