objective-c 如果键以 @ 符号开头,则在 NSDictionary 上使用 valueForKeyPath 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1567850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using valueForKeyPath on NSDictionary if a key starts the @ symbol?
提问by Michael Waterfall
I want to use valueForKeyPathon 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.
我想valueForKeyPath在我的上使用NSDictionary,但问题是其中一个键是以 @ 符号开头的字符串。我无法控制密钥的命名。
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
麦克风
回答by pxl
you shouldn't be using @ signs with your key names if you want to use key value coding.
如果要使用键值编码,则不应在键名中使用 @ 符号。
apple's guidelinesfor key names are as follows:
Keys must use ASCII encoding, begin with a lowercase letter, and may not contain whitespace.
键必须使用 ASCII 编码,以小写字母开头,并且不能包含空格。
You'll have to find a workaround to reformat the key string whereever you're getting your keys from to be KVC compliant.
无论您从何处获取密钥,都必须找到一种解决方法来重新格式化密钥字符串,以使其符合 KVC 标准。
回答by TechZen
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 operatorsas 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.
...工作,因为单个键不会作为键路径处理。
回答by Kendall Helmstetter Gelner
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?
如果您无法控制命名,那么如何添加一个带有正确命名键的类别,该键仅返回/设置奇怪的键?
回答by onmyway133
I see that there are 2 ways
我看到有两种方法
Swizzle
调酒
You can swizzle the valueForKeyPathon NSDictionaryto remove the @ symbol, remember to account for @sum, @average, ...
您可以调酒的valueForKeyPath上NSDictionary删除@符号,记住要占@sum,@average,...
Override if you're using Mantle
如果您使用的是 Mantle,则覆盖
Override + (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionaryon MTLJSONAdapter, traverse all the keys and remove the @ symbol
覆盖+ (id)modelOfClass:(Class)modelClass fromJSONDictionary:(NSDictionary *)JSONDictionaryon MTLJSONAdapter,遍历所有键并删除 @ 符号

