NSPredicateWithFormat predicate: argumentArray: only evaluate the first argument

I'm having problems using NSPredicate predicateWithFormat:argumentArray :. In this example, serverIDList is an array of strings. The result 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 serverIDList array. I can't seem to get the predicate to evaluate the whole array. Is something missing here?

Thanks!

+6
source share
1 answer
 [NSPredicate predicateWithFormat:@"(flid IN %@)" argumentArray:serverIDList] 

equivalently

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

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

What you might want is

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

Note. I would recommend not creating predicates as strings first. Chances for quotes or elusive mistakes are pretty high. Use only predicateWithFormat with a constant format string. If you need to combine predicates dynamically at run time, use NSCompoundPredicate .

+26
source

Source: https://habr.com/ru/post/951037/


All Articles