ios - NSMutableDictionary setValue :forUndefinedKey

标签 ios objective-c nsmutabledictionary key-value-observing kvc

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

相关文章:

ios - 如何创建更干净的 UIViewController?

iphone - 图标 iPhone 应用程序

objective-c - Objective C - 1 个 .h 文件需要 2 个 .m 文件?

ios - 从另一个不是 View 的类向主场景添加一个 SKSpriteNode

swift - 如何在 NSUserDefault 中快速设置字典?

ios - plist、数组和字典的元素之间是什么关系?

iOS - 串联基于 block 的异步操作

ios - UICollectionView 选择和取消选择

ios - self.tableView reloadData 在 UISplitViewController 主细节应用程序中不起作用

ios - 如何添加objectAtIndex :i of NSArray to NSMutableDictionary