使用@avg收集运算符格式化NSPredicate的字符串 [英] Format string for NSPredicate with @avg collection operator

查看:125
本文介绍了使用@avg收集运算符格式化NSPredicate的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法来构造一个NSPredicate,所以下面的数组可以过滤平均分数大于,比如5?

Is there a way to construct an NSPredicate so the following array can be filtered by average score larger than, say, 5?

NSArray *objs = @[
@{@"scores":@[@3, @5, @2]},
@{@"scores":@[@5, @2, @8, @9]},
@{@"scores":@[@7, @1, @4]}
];

我尝试过各种组合,其中一个看起来最有前途code> @ avg.self 用于通过正常KVC获取数组中数字的平均值):

I have tried various combinations, of which this one seemed the most promising (considering that the key path @avg.self works to obtain the average value of numbers in an array through normal KVC):

NSPredicate *pred = [NSPredicate predicateWithFormat:@"scores.@avg.self > 5"];
NSArray     *filterd = [objs filteredArrayUsingPredicate:pred];

我得到的运行时错误如下:

The runtime error I get is the following:

NSUnknownKeyException',reason:'[< __ NSArrayI 0x10011b7c0> valueForUndefinedKey:]:此类不是键值编码符合关键字avg。

NSUnknownKeyException', reason: '[<__NSArrayI 0x10011b7c0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key avg.

string works: scores。@ count> 3 ,所以至少可以在谓词中使用该集合运算符。

This predicate string works: scores.@count > 3, so at least that collection operator can be used in a predicate.

推荐答案

如果您更改 score。@ avg.self> 5 得分。@ avg.doubleValue> 5 它会正常工作。

If you change scores.@avg.self > 5 to scores.@avg.doubleValue > 5 it will work fine.

这篇关于使用@avg收集运算符格式化NSPredicate的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆