【发布时间】:2014-03-26 12:23:12
【问题描述】:
我正在尝试以一种可以在其中存储NSArrays 的方式使用NSDictionary,但我什至无法让它用于字符串。
_times = [[NSDictionary alloc] init];
NSString *test = @"Test";
[_times setValue:@"testing" forKey:test];
NSLog(@"%@",[_times objectForKey:test]);
对于上面的代码,我收到错误消息 Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x8b86540> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key Test.'
为什么NSString * 不能用作密钥?事实上,这正是该方法所要求的。
【问题讨论】:
-
把我的 5 美分留在这里,以防有人在使用
setValue:forKeyPath:时收到此错误,您应该知道: - 即使您的顶级字典对象可能是可变的 (NSMutableDictionary),- 那个您尝试通过keyPath访问可能仍然是不可变的 (NSDictionary),这确实会导致上述错误。
标签: ios objective-c nsstring nsdictionary