【问题标题】:NSPredicate predicateWithFormat:argumentArray: Only Evaluating First ArgumentNSPredicate predicateWithFormat:argumentArray: 只评估第一个参数
【发布时间】:2013-08-06 05:16:15
【问题描述】:

我在使用 NSPredicate 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 的第一个值。我似乎无法让谓词来评估整个数组。这里有什么遗漏吗?

谢谢!

【问题讨论】:

    标签: ios core-data nspredicate


    【解决方案1】:
    [NSPredicate predicateWithFormat:@"(flid IN %@)" argumentArray:serverIDList]
    

    等价于

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

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

    你可能想要的是

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

    备注:我会建议 not 先将谓词创建为字符串。的机会 引用或转义错误非常高。仅将 predicateWithFormat 与 常量格式字符串。如果您必须在运行时动态组合谓词, 使用NSCompoundPredicate

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 2012-02-28
      • 1970-01-01
      相关资源
      最近更新 更多