NSPredicate predicateWithFormat:argumentArray:仅评估第一个参数 [英] NSPredicate predicateWithFormat:argumentArray: Only Evaluating First Argument

查看:663
本文介绍了NSPredicate predicateWithFormat:argumentArray:仅评估第一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦使用 NSPredicate predicateWithFormat:argumentArray :。在这个例子中, serverIDList 是字符串数组。结果是具有名为flid的属性的 NSManagedObjects 数组,它是一个字符串。

  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] 


$ b b

备注:我建议不要先将谓词创建为字符串。
引用或转义错误的机会相当高。只能使用
常量格式字符串使用 predicateWithFormat 。如果你必须在运行时动态合并谓词,
使用 NSCompoundPredicate


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!

解决方案

[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.

这篇关于NSPredicate predicateWithFormat:argumentArray:仅评估第一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆