iOS: NSPredicate doesn't work correct

≡放荡痞女 提交于 2019-12-13 04:13:22

问题


I try to express the search parameters with predicate:

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

I try to express search parameters for possible string records ONLY where continent.paid==YES. The problem is that last expression is ignored. The search returns data from continent.paid==NO as well with the correct data. Whats wrong with it?


回答1:


You have to set parentheses:

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

As far as I know, "AND" and "OR" have the same precedence in a predicate, and are evaluated from left to right. Therefore in your code, the predicate evaluates to true if one of the "CONTAINS" expressions is true.



来源:https://stackoverflow.com/questions/22514335/ios-nspredicate-doesnt-work-correct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!