在谓词中使用@ min,@ max等?

编程入门 行业动态 更新时间:2024-10-28 20:20:20
本文介绍了在谓词中使用@ min,@ max等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以在谓词中使用@min等聚合运算符?

Is it possible to use aggregate operator such as @min inside a predicate?

BTW谓词过滤对象数组。

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"];

到目前为止,我只收到有关@min不符合键值的对象的错误。

So far I only get errors about the objects not being key value compliant for "@min".

谢谢

推荐答案

您收到该错误的原因是谓词被应用于 myArray 中的每个对象,并且这些对象显然不支持键路径 @min.score 。不过,NSPredicate确实支持至少一些收集运营商。这是一个有效的简单示例:

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:

NSDictionary *d1 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:35] forKey:@"score"]; NSDictionary *d2 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:52] forKey:@"score"]; NSDictionary *d3 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:13] forKey:@"score"]; NSDictionary *d4 = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:19] forKey:@"score"]; NSArray *array = [NSArray arrayWithObjects:d1, d2, d3, d4, nil]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.score == %@.@min.score", array]; NSLog(@"Min dictionaries: %@", [array filteredArrayUsingPredicate:predicate]);

在这种情况下,您可以看到 @min.score 关键路径应用于数组,这是有道理的。输出是一个包含一个包含最小值的字典的数组:

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:

Min dictionaries: ( { score = 13; } )

更多推荐

在谓词中使用@ min,@ max等?

本文发布于:2023-11-30 14:39:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650360.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:谓词   min   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!