我正在开发一个 iOS 应用程序。我有一个扩展 NSObject 的类,它定义了 1 个属性
//.h file
@property (nonatomic,retain) NSMutableDictionary *livedata;
//.m file
if(self = [super init]){
self.livedata = [[NSMutableDictionary alloc] init];
[self.livedata setValue:@"myvalue" forUndefinedKey:@"myUndefinedKey"];
}
我遇到了这个错误。 * 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类与键 myUndefinedKey 的键值编码不兼容。”
我过去曾将 NSMutableDictionary 用于 kvc、kvo。是的,我看到该类不符合 kvc。
最佳答案
问题是您调用的方法在调用时会引发异常。 setValue:forUndefinedKey:
仅在 setValue:forKey:
时调用,引用 this article , 找不到给定键的属性。相反,只需调用
[self.livedata setValue:@"myvalue" forKey:@"myKey"];
关于ios - NSMutableDictionary setValue :forUndefinedKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25352631/