或逻辑运算符不在[NSPredicate predicateWithFormat:]中工作

时间:2015-07-20 19:03:27

标签: ios objective-c core-data nspredicate

我正在尝试过滤Core Data个实体。

||中尝试使用或NSPredicate逻辑运算符时,我得到谓词解析错误。

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

使用此过滤时,没问题:

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

使用此过滤时,没问题:

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

这给出了解析错误:

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

1 个答案:

答案 0 :(得分:4)

您可以使用NSCompoundPredicate之类的

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