I'm trying to use NSDictionary
in a way that I can store NSArrays
in it, but I can't even get it working for strings.
_times = [[NSDictionary alloc] init];
NSString *test = @"Test";
[_times setValue:@"testing" forKey:test];
NSLog(@"%@",[_times objectForKey:test]);
For the code above, I get the error message Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x8b86540> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Test.'
Why would an NSString *
not work as a key? In fact, that's precisely what the method calls for.
NSDictionary
is immutable, so you can't set value to it.
use NSMutableDictionary
NSMutableDictionary *times = [[NSMutableDictionary alloc] init];
I am including @Martin's comment here
In addition: Use setObject:forKey
to set dictionary values. setValue:forKey
is only needed for "Key-Value Coding" magic. setObject:forKey
would also give a better error message if applied to an immutable dictionary.