NSPredicate predicateWithFormat传递属性名称 [英] NSPredicate predicateWithFormat passing in name of attribute

查看:136
本文介绍了NSPredicate predicateWithFormat传递属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于NSPredicate的简单问题。我试图用如下所示的传递值构造我的谓词:

Simple question regarding NSPredicate's. I'm trying to construct my predicate with "passed in" values like so :

NSPredicate* currentPredicate = [NSPredicate predicateWithFormat:@"%@ == %@",key,[changesDict valueForKey:@"Id"] ];

但是,我无法正常工作。如果我插入实际值,我通过它的确工作,但。所以这工作:

However, I haven't been able to get this to work correctly. If I insert the actual value I pass through it does work though. So this works :

NSPredicate* currentPredicate = [NSPredicate predicateWithFormat:@"contactId == %@",[changesDict valueForKey:@"Id"] ];

(注意,我插入了contactId,而不是前面的例子,

(Notice i inserted contactId as opposed to the previous example where I pass a string by the same name)

要排除故障我NSLogged两个谓词查看他们的描述,他们是不同的。我会在下面显示。

To troubleshoot I NSLogged the two predicates looking at their descriptions and they were different. I'll show them below.

这是工作中的

2013-01-17 10:29:25.513 TestingCoreData[1776:3b03] contactId == "5878"

这是不工作的

2013-01-17 10:29:25.513 TestingCoreData[1776:3b03] "contactId" == "5878"

所以我可以看到插入一个字符串,其中我真正想要的属性的名称,我稍后将在获取请求中使用。但是有什么方法可以通过传递值来实现这一点。

So I can kind of see that it's inserting a string where I really just want the name of the attribute that i'll later be using in the fetch request. But is there any way to accomplish this by passing in values?

推荐答案

>关键路径,您必须使用%K 格式:

[NSPredicate predicateWithFormat:@"%K == %@", key, [changesDict valueForKey:@"Id"]];

(请参阅解析器基础。)

这篇关于NSPredicate predicateWithFormat传递属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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