NSPredicate with AND OR and NOT condition

suggest change

A conditional predicate is cleaner and safer with NSCompoundPredicate, which provides the basic Boolean operators.

Objective-C

AND - Condition

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"samplePredicate"];
NSPredicate *anotherPredicate = [NSPredicate predicateWithFormat:@"anotherPredicate"];
NSPredicate *combinedPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[predicate, anotherPredicate]];

OR - Condition

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"samplePredicate"];
 NSPredicate *anotherPredicate = [NSPredicate predicateWithFormat:@"anotherPredicate"];
 NSPredicate *combinedPredicate = [NSCompoundPredicate orPredicateWithSubpredicates: @[predicate,anotherPredicate]];

NOT - Condition

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"samplePredicate"];
NSPredicate *combinedPredicate = [NSCompoundPredicate notPredicateWithSubpredicate:predicate];

notPredicateWithSubpredicate: takes one predicate. It does not take an array. andPredicateWithSubpredicates: and orPredicateWithSubpredicates: do.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents