对于同步服务,我试图使用谓词尽可能多地减少循环。
我目前有这样的事情:
for (NSString *filterOutPath in toFilterOutPaths) {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT (href CONTAINS %@)", filterOutPath];
toUpdateFiles = [toUpdateFiles filteredArrayUsingPredicate:pred];
}
我的目标是更像这样的东西:
NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT (href CONTAINS (ANY IN %@))", toFilterOutPaths];
toUpdateFiles = [toUpdateFiles filteredArrayUsingPredicate:pred];
有什么想法吗?非常感谢您的所有贡献!
答案 0 :(得分:2)
首先,为什么要消除循环?你认为它会跑得更快吗?如果是这样,为什么?运行几个简单谓词可能与运行一个复杂谓词一样快。
其次,你不能以你提出的方式组合CONTAINS,ANY和IN。相反,您可以创建一个这样的复合谓词:
NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:toFilterOutPaths.count];
for (NSString *filterOutPath in toFilterOutPaths) {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"NOT (href CONTAINS %@)", filterOutPath];
[predicates addObject:predicate];
}
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
toUpdateFiles = [toUpdateFiles filteredArrayUsingPredicate:compoundPredicate];