0

我正在开发一个 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。

4

2 回答 2

1

问题是您正在调用一个在调用时引发异常的方法。setValue:forUndefinedKey:仅在setValue:forKey:引用这篇文章时没有找到给定键的属性时调用。相反,只需调用

[self.livedata setValue:@"myvalue" forKey:@"myKey"];
于 2014-08-17T19:05:28.570 回答
0

setValue:forKey:不想setValue:forUndefinedKey:

[self.livedata setValue:@"value" forKey:@"key"];

系统调用setValue:forUndefinedKey和方法可用于覆盖。

来自 Docs:当它找不到给定键的属性时
调用。setValue:forKey:

“子类可以重写此方法以其他方式处理请求。默认实现引发NSUndefinedKeyException.”

于 2014-08-17T19:12:27.033 回答