NSPredicate with AND OR and NOT condition
suggest changeConditional predicate will be cleaner and safer by using the NSCompoundPredicate
class which provides basic boolean operators for the given predicates.
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 *anotherPredicate = [NSPredicate predicateWithFormat:@"anotherPredicate"];
NSPredicate *combinedPredicate = [NSCompoundPredicate notPredicateWithSubpredicate: @[predicate,anotherPredicate]];
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents