Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate with Multiple Conditions

I am trying to create a NSPredicate with multiple conditions. I've found several solutions, but none of them appear to be working with my method. The best looking one I've found is below.

This is my single predicate method, and it works just fine:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", 
searchText];
filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];

Here is my edited version with multiple conditions. I'm not sure whats going wrong. Any ideas?

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
NSPredicate *p2 = [NSPredicate predicateWithFormat:@"businessArea contains[c] %@", 
searchText];

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];
filteredBusinesses = [businesses filteredArrayUsingPredicate:predicate];
like image 323
Brandon Avatar asked Sep 03 '14 03:09

Brandon


1 Answers

You can try this

NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2]];
like image 102
Nikunj Agola Avatar answered Oct 07 '22 20:10

Nikunj Agola