gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:48:07 25 4
gpt4 key购买 nike

是否可以在谓词中使用诸如@min 之类的聚合运算符?

BTW 谓词过滤对象数组。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY SELF.score == @min.score"];

我知道您可以使用键值运算符检索最小值,例如:

NSNumber *minScore = [myArray valueForKeyPath:@"@min.score"];

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

谢谢

最佳答案

您收到该错误的原因是谓词被应用于 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 == %@.@min.score", array];
NSLog(@"Min dictionaries: %@", [array filteredArrayUsingPredicate:predicate]);

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

Min dictionaries: (
{
score = 13;
}
)

关于iphone - 在谓词中使用@min、@max 等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6515167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com