IPhone - NSPredicate (add additional filters)

I have this predicate

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

At some point, if the logic test is valid, I would like to add a third filter to this predicate, something like

 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]; } 

I believe that I can create an NSMutableString for the format and add a third part if necessary, but I suspect there is a way to do this using NSPredicate in a more elegant way.

How can I do it? Is it possible?

thanks

+4
source share
1 answer

Take a look at NSCompoundPredicate , which allows you to build an instance of NSPredicate from one or more sub-editions:

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

All Articles