如果键以 @ 符号开头,则在 NSDictionary 上使用 valueForKeyPath?
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想使用键值编码,则不应在键名称中使用 @ 符号。
Apple 的按键名称指南是如下:
无论您从何处获取密钥,都必须找到一种解决方法来重新格式化密钥字符串,以使其符合 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:
You'll have to find a workaround to reformat the key string whereever you're getting your keys from to be KVC compliant.
只是稍微更新一下这个老问题...
这些的原因:
...失败是关键路径中的任何“@”符号都被解释为 集合的运算符 如下:
嵌套键调用:
... 起作用,因为单个键不被处理为键路径。
Just to update this old question a little...
The reason that these:
...fail is that any "@" symbols in a key path are interpreted as being collection's operators as with:
The nested key calls:
... work because a single key is not processed as a key path.
如果您无法控制命名,那么如何添加一个具有正确命名的键的类别,该键仅返回/设置奇怪的键?
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?
我看到有两种方法
Swizzle
你可以在
NSDictionary
上 swizzlevalueForKeyPath
来删除@符号,记住要考虑@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
onNSDictionary
to remove the @ symbol, remember to account for @sum, @average, ...Override if you're using Mantle
Override
+ (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionary
onMTLJSONAdapter
, traverse all the keys and remove the @ symbol以我的拙见,这里的整个讨论都走错了方向通过关键路径访问 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).