NS predicate和数组 [英] NSPredicate and arrays

查看:109
本文介绍了NS predicate和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简短的问题。我有一个的NSArray 充满汽车(从NSObject的继承)。 @财产的NSString *引擎(也算是 @synthesize

I've got a short question. I have an NSArray filled with Cars (which inherits from NSObject). Car has the @property NSString *engine (also regarded @synthesize)

现在我想用数组来过滤 NS predicate

Now I want to filter the array using NSPredicate:

predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(engine like %@)", searchText]];
newArray = [ArrayWithCars filteredArrayUsingPredicate:predicate];

这将引发 valueForUndefinedKey 错误。是 predicateWithFormat 是否正确?

This throws an valueForUndefinedKey error. Is the predicateWithFormat correct?

感谢您的答复。

推荐答案

这也是为什么它不工作的真正原因:

Here's the real reason why it's not working:

当你建立一个使用 stringWithFormat的字符串:,它会拿出看起来像这样:

When you build a string using stringWithFormat:, it's going to come up looking like this:

@"engine like searchText"

您然后将它传递给 predicateWithFormat:,这是会看到,无论是左手侧和比较的右侧是的不带引号的字符串,这意味着它要跨preT他们,如果他们的 keypaths

You then pass it to predicateWithFormat:, which is going to see that both the lefthand side and the righthand side of the comparison are unquoted strings, which means it's going to interpret them as if they were keypaths.

在此code运行时,它会做:

When this code runs, it's going to be doing:

id leftValue = [object valueForKey:@"engine"];
id rightValue = [object valueForKey:@"searchText"];
return (leftValue is like rightValue);

该异常抛出得到的是抱怨的对象,它并没有一个名为SEARCHTEXT的方法,这是正确的。

The exception getting thrown is your object complaining that it doesn't have a method named "searchText", which is correct.

与此不同,如果你拿出 stringWithFormat:通话,只是把一切都直接进入 predicateWithFormat:

Contrast this to if you took out the stringWithFormat: call and just put everything directly into predicateWithFormat::

NSPredicate *p = [NSPredicate predicateWithFormat:@"engine like %@", searchText];

predicateWithFormat:是聪明的。它认为啊哈,一个%@ 替换模式!这意味着我可以在弹出的参数关闭参数列表和盒子它和使用它按原样。这正是它要做的事情。这将弹出 SEARCHTEXT 的值off参数列表,在框成为一个 NSEx pression ,和直接将其插入前解析pression树。

predicateWithFormat: is smart. It sees "aha, a %@ replacement pattern! this means I can pop an argument off the argument list and box it up and use it as-is". That's exactly what it's going to do. It's going to pop the value of searchText off the argument list, box it in an NSExpression, and insert it directly into the parsed expression tree.

什么是最有趣的是在这里将其创建的前pression将是一个的恒定值的前pression。这意味着它是一个文本值;不是关键路径,而不是一个集等,这将是文字串。那么当你的predicate运行时,它会做:

What's most interesting here is that the expression it creates will be a constant value expression. This means it's a literal value; not a key path, not a collection, etc. It will be the literal string. Then when your predicate runs, it's going to do:

id leftValue = [object valueForKey:@"engine"];
id rightValue = [rightExpression constantValue];
return (leftValue is like rightValue);

的是正确的,也是为什么你不应该通过 stringWithFormat传递的东西:它传递给之前 predicateWithFormat:

And that is correct, and is also why you should not pass stuff through stringWithFormat: before passing it to predicateWithFormat:.

这篇关于NS predicate和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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