This question already has an answer here:
这个问题已经有了答案:
- Error getting valueForKey in an NSDictionary for key containing @ character 2 answers
- 在NSDictionary中获取包含@字符2答案的键的valueForKey时出错
I am having trouble with RestKit
when I had a JSON response with @
symbols in the keys. After some debugging it seems the issue is happening in __NSCFDictionary
当我有一个带有@符号的JSON响应时,我在RestKit上遇到了麻烦。经过一些调试之后,问题似乎正在__NSCFDictionary中发生
So I tried the following simple code:
所以我尝试了以下简单的代码:
NSArray *keys = [NSArray arrayWithObjects:@"@key1", @"@key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
for (id key in dictionary) {
NSLog(@"key: %@, value: %@", key, [dictionary valueForKey:key]);
}
And I am getting the following error:
我得到的错误如下:
[<__NSDictionaryI 0x618000268ac0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key key1.
[<__NSDictionaryI 0x618000268ac0> valueForUndefinedKey:]:这个类不支持键key1的键值编码。
Can someone please explain why I am getting this error and if there is any workaround?
有人能解释一下我为什么会犯这个错误吗?
2 个解决方案
#1
12
You can't use @
in the keys in conjunction with valueForKey:
. NSDictionary
has some documented but perhaps unexpected behavior in that case: it strips the @
and invokes [super valueForKey:]
with the new key. That looks for the key on the object, not in the dictionary's contents. No such key exists on instances of NSDictionary
, so an exception is raised.
你不能在键中使用@与valueForKey:。在这种情况下,NSDictionary有一些记录在案但可能是意外的行为:它去掉@并使用新键调用[super valueForKey:]。查找对象上的键,而不是字典的内容。在NSDictionary的实例中不存在这样的键,因此会引发异常。
You should in general use objectForKey:
to retrieve values from an NSDictionary
.
您通常应该使用objectForKey:从NSDictionary中检索值。
Credit must go to Ken Thomases for his comments below, correcting earlier versions of this answer.
下面的评论必须归功于肯·托马斯,他纠正了这个答案的早期版本。
#2
0
Because you copied the code of someone who uses valueForKey: instead of objectForKey: without understanding what each is good for.
因为您复制了使用valueForKey:而不是objectForKey的人的代码:而不理解它们的好处。
objectForKey: is what you should use. It takes the key that you pass in, and looks up the value for that key.
objectForKey:是您应该使用的。它使用你传入的键,并查找该键的值。
valueForKey: is a highly complicated method that looks at the text in the key, and does all kind of complicated things, for example a key of @"@sum" will add up some values. If that is what you want, fine. If you want to look up a value, use objectForKey. It does what you think it should do, and is usually several times faster than valueForKey: anyway.
valueForKey:是一个非常复杂的方法,它查看键中的文本,并执行各种复杂的操作,例如@"@ "@sum"的键将加一些值。如果这是你想要的,没问题。如果您想查找一个值,请使用objectForKey。它做你认为它应该做的事,而且通常比valueForKey快几倍。
As a rule of thumb: If I asked you whether you should use objectForKey: or valueForKey:, and you don't know what the difference is, use objectForKey:
作为一个经验法则:如果我问你是否应该使用objectForKey:或valueForKey:,而你不知道两者之间的区别是什么,使用objectForKey:
#1
12
You can't use @
in the keys in conjunction with valueForKey:
. NSDictionary
has some documented but perhaps unexpected behavior in that case: it strips the @
and invokes [super valueForKey:]
with the new key. That looks for the key on the object, not in the dictionary's contents. No such key exists on instances of NSDictionary
, so an exception is raised.
你不能在键中使用@与valueForKey:。在这种情况下,NSDictionary有一些记录在案但可能是意外的行为:它去掉@并使用新键调用[super valueForKey:]。查找对象上的键,而不是字典的内容。在NSDictionary的实例中不存在这样的键,因此会引发异常。
You should in general use objectForKey:
to retrieve values from an NSDictionary
.
您通常应该使用objectForKey:从NSDictionary中检索值。
Credit must go to Ken Thomases for his comments below, correcting earlier versions of this answer.
下面的评论必须归功于肯·托马斯,他纠正了这个答案的早期版本。
#2
0
Because you copied the code of someone who uses valueForKey: instead of objectForKey: without understanding what each is good for.
因为您复制了使用valueForKey:而不是objectForKey的人的代码:而不理解它们的好处。
objectForKey: is what you should use. It takes the key that you pass in, and looks up the value for that key.
objectForKey:是您应该使用的。它使用你传入的键,并查找该键的值。
valueForKey: is a highly complicated method that looks at the text in the key, and does all kind of complicated things, for example a key of @"@sum" will add up some values. If that is what you want, fine. If you want to look up a value, use objectForKey. It does what you think it should do, and is usually several times faster than valueForKey: anyway.
valueForKey:是一个非常复杂的方法,它查看键中的文本,并执行各种复杂的操作,例如@"@ "@sum"的键将加一些值。如果这是你想要的,没问题。如果您想查找一个值,请使用objectForKey。它做你认为它应该做的事,而且通常比valueForKey快几倍。
As a rule of thumb: If I asked you whether you should use objectForKey: or valueForKey:, and you don't know what the difference is, use objectForKey:
作为一个经验法则:如果我问你是否应该使用objectForKey:或valueForKey:,而你不知道两者之间的区别是什么,使用objectForKey: