2011-09-07 59 views
4

我有这个谓词iPhone - NSPredicate(增加更多的过滤器)

NSPredicate *myPredicate = 
    [NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@)", date, isEnabled]; 

在某些时候,如果一个布尔测试是有效的,我想第三个过滤器添加到这个谓词,像

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以更优雅的方式进行操作。

我该怎么做?可能吗?

感谢

回答

4

看看NSCompoundPredicate让您从一个或多个subpredicates构建出的NSPredicate一个实例:

NSArray *subpreds = [NSArray arrayWithObjects:existingPredicate, [NSPredicate predicateWithFormat:...], nil]; 
NSPredicate *finished = [NSCompoundPredicate andPredicateWithSubpredicates:subpreds]; 
+0

Yessssssssssss !!!!谢谢!而已! – SpaceDog