如果键以 @ 符号开头,则在 NSDictionary 上使用 valueForKeyPath?

发布于 2024-08-08 08:33:28 字数 613 浏览 2 评论 0原文

我想在我的 NSDictionary 上使用 valueForKeyPath ,但问题是其中一个键是以 @ 符号开头的字符串。我无法控制密钥的命名。

我在尝试创建关键路径时遇到问题,因为我收到格式异常,即使在尝试转义 @ 符号时也是如此:

这工作正常:

[[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]

但是这些都不起作用:< /strong>

[dict valueForKeyPath:@"[email protected]"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]

有什么想法吗?

谢谢,

迈克

I want to use valueForKeyPath on my NSDictionary, but the problem is that one of the keys is a string that starts with the @ symbol. I have no control over the naming of the key.

I'm having problems trying to create the key path as I'm getting a format exception, even when trying to escape the @ symbol:

This works fine:

[[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]

However none of these work:

[dict valueForKeyPath:@"[email protected]"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]

Any ideas?

Thanks,

Mike

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

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

发布评论

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

评论(5

难如初 2024-08-15 08:33:28

如果您想使用键值编码,则不应在键名称中使用 @ 符号。

Apple 的按键名称指南是如下:

按键必须使用 ASCII 编码,开始
带小写字母,并且可能不
包含空格。

无论您从何处获取密钥,都必须找到一种解决方法来重新格式化密钥字符串,以使其符合 KVC 标准。

you shouldn't be using @ signs with your key names if you want to use key value coding.

apple's guidelines for key names are as follows:

Keys must use ASCII encoding, begin
with a lowercase letter, and may not
contain whitespace.

You'll have to find a workaround to reformat the key string whereever you're getting your keys from to be KVC compliant.

盛夏尉蓝 2024-08-15 08:33:28

只是稍微更新一下这个老问题...

这些的原因:

[dict valueForKeyPath:@"[email protected]"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]

...失败是关键路径中的任何“@”符号都被解释为 集合的运算符 如下:

[dict valueForKeyPath:@"[email protected]"] // returns the sum of all 'key3' values
[dict valueForKeyPath:@"[email protected]"] // returns the average of all 'key3' values

嵌套键调用:

[[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]

... 起作用,因为单个键不被处理为键路径。

Just to update this old question a little...

The reason that these:

[dict valueForKeyPath:@"[email protected]"]
[dict valueForKeyPath:@"key1.@@specialKey.key3"]

...fail is that any "@" symbols in a key path are interpreted as being collection's operators as with:

[dict valueForKeyPath:@"[email protected]"] // returns the sum of all 'key3' values
[dict valueForKeyPath:@"[email protected]"] // returns the average of all 'key3' values

The nested key calls:

[[[dict objectForKey:@"key1"] objectForKey:@"@specialKey"] objectForKey:@"key3"]

... work because a single key is not processed as a key path.

决绝 2024-08-15 08:33:28

如果您无法控制命名,那么如何添加一个具有正确命名的键的类别,该键仅返回/设置奇怪的键?

If you have no control over the naming, how about adding a category with a properly named key that simply returns/sets the weird key?

旧时光的容颜 2024-08-15 08:33:28

我看到有两种方法

Swizzle

你可以在 NSDictionary 上 swizzle valueForKeyPath 来删除@符号,记住要考虑@sum,@平均,...

如果您使用 Mantle,则覆盖

MTLJSONAdapter< 上覆盖 + (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionary /code>,遍历所有的key,去掉@符号

I see that there are 2 ways

Swizzle

You can swizzle the valueForKeyPath on NSDictionary to remove the @ symbol, remember to account for @sum, @average, ...

Override if you're using Mantle

Override + (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionary on MTLJSONAdapter, traverse all the keys and remove the @ symbol

你好,陌生人 2024-08-15 08:33:28

以我的拙见,这里的整个讨论都走错了方向通过关键路径访问 NSDictionary 中的条目 - 根本不是 KVC 协议的一部分。

KVC 定义了如何命名对象的属性,以便 KVC 可以工作。 NSDictionary 中的条目不是属性,也没有名称。 NSDictionary 通过“假装”其条目的键就像字典的“属性”,为类似 KVC 的行为添加了一些魔力。

唉,属性与字典键相比具有不同的命名约定和限制。

如果您无法强制字典键符合 KVC 支持的属性名称 - 中断您的键路径,并在有疑问的地方使用访问器。

我认为,这应该是最安全的方法。 KVC 通常是一个“很好”的功能,能够缩短您的代码 - 但它不提供任何您无法使用的功能(正如您自己演示的那样)。

In my humble opinion, the whole discussion here goes the wrong way Accessing entries in an NSDictionary via key paths - is simply not part of KVC protocol.

KVC defines how to name your properties of an object, so that KVC can work. an entry in an NSDictionary is not a property, and has no name. NSDictionary adds its bit of magic to the KVC-like behaviour, by "pretending" the keys of its entries are like 'properties' of the dictionary.

Alas, properties have different naming conventions and limitations than dictionary keys.

If you cannot force the dictionary keys to conform with KVC-supported property names - break your key paths, and use the accessors instead where in doubt.

That, I think, should be the safest way to go. KVC is generally a "nicety" being able to shorten your code - but it does NOT provide any functionality you cannot have otherwise (as you demonstrated yourself).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文