谓词中对象的字典属性的元素的键路径?

发布于 2024-12-06 03:45:50 字数 404 浏览 1 评论 0原文

简而言之,我正在寻找正确的谓词格式来索引对象的字典属性。

我有一个 myObject 实例数组,每个实例都有一个属性 CPDictionary(NSMutableDictionary) extAttributes,我试图允许根据 extAttributes 中的值进行过滤。我定义我的谓词如下,

[CPPredicate predicateWithFormat:@"extAttributes[%K] like[c] %@",key,val]

我不确定正确的键路径是什么。阅读 NSPredicate 上的文档,似乎“extAttributes[%k]”可能是数组的索引,而不是字典。我尝试了几种替代方案,这是唯一一个不会出错的方案。检查结果对象时,谓词对象中的表达式似乎是正确的。然而,它没有达到预期的结果。

In short, I am looking for the correct predicate format to index into a dictionary attribute on an object.

I have an array of myObject instances, each with an attribute CPDictionary(NSMutableDictionary) extAttributes and I am trying to allow filtering based on values in extAttributes. I am defining my predicate as follows

[CPPredicate predicateWithFormat:@"extAttributes[%K] like[c] %@",key,val]

I am not sure what the correct key-path would be for this. Reading the documentation on NSPredicate it would seem that "extAttributes[%k]" might be an index into an array, not a dictionary. I have tried several alternatives and this is the only one that doesn't error. When inspecting the resulting objects, the expressions in the predicate object appear to be correct. However, it doesn't have the intended result.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡の花香 2024-12-13 03:45:50

看起来您可能会混淆数组和字典引用。只需使用关键路径(点分隔的元素名称)来引用嵌套元素或对象属性,概念上是这样的:

[CPPredicate predicateWithFormat:@"%K like[c] %@", @"extAttributes.myNestedElementName", val];

话又说回来,我对卡布奇诺不太熟悉,所以我不确定他们是否已经完成了这里略有不同,但这就是 Cocoa 和 Cocoa touch 中的工作方式。

编辑

要动态计算密钥,您需要做的就是编写一个密钥路径字符串:

 var keyPath = [CPString stringWithFormat:@"%@.%@", @"extAttributes", key];

您现在可以按如下方式创建谓词:

[CPPredicate predicateWithFormat:@"%K like[c] %@", keyPath, val];

编辑

Cocoa/Cocoa 中的等效代码触摸将是:

NSString *keyPath = [NSString stringWithFormat:@"%@.%@", @"extAttributes", key];
[NSPredicate predicateWithFormat:@"%K like[c] %@", keyPath, val];

It looks like you might be confusing array and dictionary references. Just use a key path (dot-separated element names) to refer to nested elements or object properties, conceptually something like this:

[CPPredicate predicateWithFormat:@"%K like[c] %@", @"extAttributes.myNestedElementName", val];

Then again, I'm not all that familiar with Cappuccino, so I'm not sure if they've done something slightly different here, but this is the way it works in Cocoa and Cocoa touch.

EDIT

To compute the key dynamically, all you need to do is compose a key path string:

 var keyPath = [CPString stringWithFormat:@"%@.%@", @"extAttributes", key];

You can now create the predicate as follows:

[CPPredicate predicateWithFormat:@"%K like[c] %@", keyPath, val];

EDIT

The equivalent code in Cocoa/Cocoa touch would be:

NSString *keyPath = [NSString stringWithFormat:@"%@.%@", @"extAttributes", key];
[NSPredicate predicateWithFormat:@"%K like[c] %@", keyPath, val];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文