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
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.
Can someone please explain why I am getting this error and if there is any workaround?
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.
You should in general use objectForKey:
to retrieve values from an NSDictionary
.
Credit must go to Ken Thomases for his comments below, correcting earlier versions of this answer.