我有这个谓词
NSPredicate *myPredicate =
[NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@)", date, isEnabled];
在某些时候,如果 bool 测试有效,我想向该谓词添加第三个过滤器,例如
if (testValid) {
// use some magic to add a third option here
// the third option is "&& (paid == %@")
// and the condition is given by isPaid
// the idea is to append a third condition on myPredicate, making it like
// myPredicate =
// [NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@) && (paid == %@)", date, isEnabled, isPaid];
}
我想我可以为该格式创建一个 NSMutableString 并在必要时附加第三部分,但我怀疑有一种方法可以以更优雅的方式使用 NSPredicate 来做到这一点。
我该怎么做?可能吗?
谢谢
最佳答案
看一下 NSCompoundPredicate
,它允许您从一个或多个子谓词构造 NSPredicate
实例:
NSArray *subpreds = [NSArray arrayWithObjects:existingPredicate, [NSPredicate predicateWithFormat:...], nil];
NSPredicate *finished = [NSCompoundPredicate andPredicateWithSubpredicates:subpreds];
关于iPhone - NSPredicate(添加更多过滤器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327941/