NSPredicate with AND OR and NOT condition
suggest changeA 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents