Add080bbA
Add080bbA

Reputation: 1876

Or logic operator not working in [NSPredicate predicateWithFormat:]

I am trying to filter Core Data entities.

When trying to use or || logic operator in NSPredicate, I get predicate parse error.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(dRIV_NAMESURNAME CONTAINS[cd] %@) || (vEHI_PLATE CONTAINS[cd] %@)"'

When filtering with this, no problem:

NSPredicate* preFilter = 
   [NSPredicate predicateWithFormat:@"dRIV_NAMESURNAME CONTAINS[cd] %@", strSearch];

When filtering with this, no problem:

NSPredicate* preFilter = 
   [NSPredicate predicateWithFormat:@"vEHI_PLATE CONTAINS[cd] %@", strSearch];

This gives parse error:

NSPredicate* preFilter = [NSPredicate predicateWithFormat:
   @"(dRIV_NAMESURNAME CONTAINS[cd] %@) || (vEHI_PLATE CONTAINS[cd] %@)",
   strSearch, strSearch];

Upvotes: 0

Views: 821

Answers (1)

Harsh Jaiswal
Harsh Jaiswal

Reputation: 266

You can use NSCompoundPredicate like

NSPredicate* filter1 = [NSPredicate predicateWithFormat:@"dRIV_NAMESURNAME CONTAINS[cd] %@", strSearch];

NSPredicate* filter2 = [NSPredicate predicateWithFormat:@"vEHI_PLATE CONTAINS[cd] %@", strSearch];

NSArray *searchFilters=@[filter1,filter2];

NSPredicate *compoundPredicate=[NSCompoundPredicate orPredicateWithSubpredicates:searchFilters];

Upvotes: 4

Related Questions