1

Possible Duplicate:
Using valueForKeyPath on NSDictionary if a key starts the @ symbol?

Error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason:
'[<__NSDictionaryI 0x100110e00> valueForUndefinedKey:]:
this class is not key value coding-compliant for the key .'

Example code:

NSDictionary *dict = @{ @"foo": @"bar" };

NSLog(@"foo=%@, qaz=%@", [dict valueForKey:@"foo"], [dict valueForKey:@"qaz"]);
NSLog(@"@=%@", [dict valueForKey:@"@"]); // SIGABRT

This also happens when using [dict objectForKey:@"@"].

Even when the "@" key is defined it still causes SIGABRT:

NSDictionary *dict = @{ @"@": @"at-sign" };
NSLog(@"@=%@", [dict valueForKey:@"@"]); // SIGABRT

Why is this happening and how can I retrieve the value for the "@" key from the dictionary?

Community
  • 1
  • 1
Tyilo
  • 28,998
  • 40
  • 113
  • 198

1 Answers1

5

You should use objectForKey: when trying to access objects in a dictionary, the valueForKey: and valueForKeyPath: methods are meant for KVC and have some restrictions for their naming. Using [dict objectForKey:@"@"] will work and return either (null) or at-sign for your examples.

JustSid
  • 25,168
  • 7
  • 79
  • 97