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

发布于 2024-11-17 09:59:21 字数 340 浏览 1 评论 0原文

是否可以在谓词内使用聚合运算符(例如@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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老旧海报 2024-11-24 09:59:21

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

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 == %@[email protected]", array];
NSLog(@"Min dictionaries: %@", [array filteredArrayUsingPredicate:predicate]);

您可以看到,在本例中,@min.score 键路径应用于数组,这是有意义的。输出是一个数组,其中包含一个包含最小值的字典:

Min dictionaries: (
        {
        score = 13;
    }
)

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 == %@[email protected]", array];
NSLog(@"Min dictionaries: %@", [array filteredArrayUsingPredicate:predicate]);

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;
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文