gpt4 book ai didi

objective-c - 带有 NSPredicate 和 SBElementArray 的 IN 运算符

转载 作者:行者123 更新时间:2023-12-03 17:03:42 24 4
gpt4 key购买 nike

IN 运算符是否可以过滤 SBElementArray?我一直在尝试使用它,但它总是返回一个 NULL 数组。

我的代码(hexArray 通常会有更多元素):

SBElementArray *musicTracks = [libraryPlaylist fileTracks];
hexArray = [NSArray arrayWithObject: @"3802BF81BD1DAB10"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K IN %@",@"persistentID",hexArray];

NSLog(@"%@", [[musicTracks filteredArrayUsingPredicate:predicate] valueForKey:@"persistentID"]);
NSLog(@"%@", hexArray);
NSLog(@"%@", predicate);

输出:

2013-05-26 12:59:29.907 test[1226:403] (null)
2013-05-26 12:59:29.907 test[1226:403] (3802BF81BD1DAB10)
2013-05-26 12:59:29.908 test[1226:403] ANY persistentID IN {"3802BF81BD1DAB10"}

我尝试将谓词设置为:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY %K == %@",@"persistentID",hexArray];

输出:

2013-05-26 13:03:04.629 test[1258:403] (3802BF81BD1DAB10)
2013-05-26 13:03:04.630 test[1258:403] (3802BF81BD1DAB10)
2013-05-26 13:03:04.630 test[1258:403] ANY persistentID == {"3802BF81BD1DAB10"}

这工作得很好。但我想要 IN 功能。

最佳答案

而不是做

persistentID IN ('abc', 'abc', 'abc', ...)

你可以做到

persistentID == 'abc' OR persistentID == 'abc' OR ...

看起来运行速度相当快。

<小时/>
NSMutableArray *subPredicates = [NSMutableArray arrayWithCapacity:persistentIDs.count];

for (NSNumber *persistentID in persistentIDs) {
[subPredicates addObject:pred(@"persistentID == %@", persistentID.hexValue)];
}

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subPredicates];
[tracks filterUsingPredicate:predicate];

NSLog(@"%ld", tracks.count);

关于objective-c - 带有 NSPredicate 和 SBElementArray 的 IN 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759079/

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