0
votes

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];
1
try leaving the parentheses out so NSPredicate* preFilter = [NSPredicate predicateWithFormat:@"dRIV_NAMESURNAME CONTAINS[cd] %@ || vEHI_PLATE CONTAINS[cd] %@", strSearch, strSearch];Drmorgan
still not working. same errorAdd080bbA
The || should work in exactly the same way as the compound predicate. Can you verify that you are using the same data & search string (e.g. not a nil strSearch)?Mundi
not nil. same input works when condition filtered seperatllyAdd080bbA

1 Answers

4
votes

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];