NSPredicate predicateWithFormat:argumentArray:仅评估第一个参数

我在使用NSPredicate predicateWithFormat:argumentArray遇到了一些麻烦predicateWithFormat:argumentArray :. 在此示例中, serverIDList是字符串数组。 结果是一个NSManagedObjects数组,其属性名为“flid”,这是一个字符串。

 NSMutableString *predicateString = [[NSMutableString alloc] init]; [predicateString appendString:@"(flid IN %@)"]; [results filterUsingPredicate:[NSPredicate predicateWithFormat:predicateString argumentArray:serverIDList]]; 

问题是[NSPredicate predicateWithFormat:predicateString argumentArray:serverIDList]求值为“flid IN’2155’”,这只是数组serverIDList的第一个值。 我似乎无法获得谓词来评估整个数组。 这里有什么东西不见了吗?

谢谢!

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

相当于

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

其中id1 ,…, idN是数组serverIDList的元素。 这应该解释为什么只评估第一个元素。

你可能想要的是什么

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

备注:我建议不要先将谓词创建为字符串。 引用或逃避错误的可能性非常高。 仅使用带有常量格式字符串的predicateWithFormat 。 如果必须在运行时动态组合谓词,请使用NSCompoundPredicate