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

关于ios - 或逻辑运算符无法在[NSPredicate predicateWithFormat:]中使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31524369/

10-10 16:49
查看更多