在谓词中使用@min、@max 等?
是否可以在谓词内使用聚合运算符(例如@min)?
BTW 谓词过滤对象数组。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.score == @min.score"];
我知道您可以使用键值运算符检索最小值,例如:
NSNumber *minScore = [myArray valueForKeyPath:@"@min.score"];
到目前为止,我只收到有关对象不符合“@min”键值的错误。
谢谢
Is it possible to use aggregate operator such as @min inside a predicate?
BTW The predicate filters an array of objects.
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.score == @min.score"];
I know you can retrieve the min value using the key-value operators eg:
NSNumber *minScore = [myArray valueForKeyPath:@"@min.score"];
So far I only get errors about the objects not being key value compliant for "@min".
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到该错误的原因是谓词应用于 myArray 中的每个对象,而这些对象显然不支持关键路径@min.score >。不过,NSPredicate 确实支持至少一些集合运算符。这是一个有效的简单示例:
您可以看到,在本例中,
@min.score
键路径应用于数组,这是有意义的。输出是一个数组,其中包含一个包含最小值的字典:The reason that you're getting that error is that the predicate is being applied to each object in
myArray
, and those objects apparently don't support the key path@min.score
. NSPredicate does have support for at least some of the collection operators, though. Here's a simple example that works:You can see that in this case, the
@min.score
key path is applied to the array, which makes sense. The output is an array containing the one dictionary that contains the minimum value: