iOS:NSPredicate不正常

时间:2014-03-19 17:59:02

标签: ios core-data nspredicate

我尝试用谓词表达搜索参数:

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"name CONTAINS [cd] %@  OR code CONTAINS [cd] %@ OR currency CONTAINS [cd] %@ AND continent.paid == %@",searchString,searchString,searchString,[NSNumber numberWithBool:YES]];

我尝试仅为continent.paid == YES表示可能的字符串记录的搜索参数。问题是忽略了最后一个表达式。搜索返回来自continent.paid == NO的数据以及正确的数据。这有什么不对吗?

1 个答案:

答案 0 :(得分:2)

你必须设置括号:

[NSPredicate predicateWithFormat:@"(name CONTAINS [cd] %@  OR code CONTAINS [cd] %@ OR currency CONTAINS [cd] %@) AND continent.paid == %@",
         searchString,searchString,searchString,[NSNumber numberWithBool:YES]];

据我所知,“AND”和“OR”在谓词中具有相同的优先级,并进行评估 从左到右。因此,在您的代码中,谓词的计算结果为true “CONTAINS”表达式是真的。