2013-08-05 53 views
6

我在使用NSPredicatepredicateWithFormat:argumentArray时遇到了一些麻烦:。在此示例中,serverIDList是字符串数组。结果是NSManagedObjects的一个数组,名为“flid”的属性是一个字符串。NSPredicate predicateWithFormat:argumentArray:仅评估第一个参数

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的第一个值。我似乎无法得到谓词来评估整个数组。这里有什么缺失吗?

谢谢!

回答

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

相当于

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

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

你可能想要的是

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

注:我会电子书籍首先创建谓词为字符串。引用或转义错误的机会相当高。使用predicateWithFormat和 常量格式字符串。如果必须在运行时动态组合谓词,则 使用NSCompoundPredicate

+0

啊啊感谢您的解释! –