或者逻辑运算符在 [NSPredicate predicateWithFormat:] 中不起作用

Or logic operator not working in [NSPredicate predicateWithFormat:]

我正在尝试过滤 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];

您可以像

一样使用 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];