NSPredicate predicateWithFormat:argumentArray: Only Evaluating First Argument

谁说胖子不能爱 提交于 2019-12-21 12:27:08

问题


I'm having some trouble using NSPredicate predicateWithFormat:argumentArray:. In this example, serverIDList is array of strings. Results is an array of NSManagedObjects with an attribute named "flid" which is a string.

NSMutableString *predicateString = [[NSMutableString alloc] init];

[predicateString appendString:@"(flid IN %@)"];

[results filterUsingPredicate:[NSPredicate predicateWithFormat:predicateString argumentArray:serverIDList]];

The problem is that [NSPredicate predicateWithFormat:predicateString argumentArray:serverIDList] evaluates to "flid IN '2155'", which is only the first value of the array serverIDList. I can't seem to get the predicate to evaluate the entire array. Is there something missing here?

Thanks!


回答1:


[NSPredicate predicateWithFormat:@"(flid IN %@)" argumentArray:serverIDList]

is equivalent to

[NSPredicate predicateWithFormat:@"(flid IN %@)", id1, id2, ..., idN]

where id1, ..., idN are the elements of the array serverIDList. That should explain why only the first element is evaluated.

What you probably want is

[NSPredicate predicateWithFormat:@"(flid IN %@)", serverIDList]

Remark: I would recomment not to create predicates as strings first. The chances for quoting or escaping errors are quite high. Use only predicateWithFormat with a constant format string. If you have to combine predicates dynamically at runtime, use NSCompoundPredicate.



来源:https://stackoverflow.com/questions/18059170/nspredicate-predicatewithformatargumentarray-only-evaluating-first-argument

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