我正在尝试在NSMutableDictionary
中设置一个值,但我总是收到此错误:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSDictionaryI 0x10fd55990> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image.'
在NSMutableDictionary中,我有一个@"image"
字段,可以是null
。我想用占位符图片名称替换null
值。
我就是这样做的:
NSMutableDictionary * item = self.carouselSource[index];
if([Utils isNull:[item valueForKey:@"image"]]) {
[item setValue:@"default_image.png" forKey:@"image"];
}
但应用程序崩溃在这一行
[item setValue:@"default_image.png" forKey:@"image"];
由于上述错误。
答案 0 :(得分:0)
您的词典不可变 - 它是 NSDictionary 而不是 NSMutableDictionary 。如果您想要转换任何(数组,词典) immutable 到 mutable 使用 mutableCopy 进行完全转换(平衡保留计数)。
NSMutableDictionary * item = [self.carouselSource[index] mutableCopy];
if([Utils isNull:[item objectForKey:@"image"]]) {
[item setObject:@"default_image.png" forKey:@"image"];
}
而不是
NSDictionary * item = self.carouselSource[index];
if([Utils isNull:[item valueForKey:@"image"]]) {
[item setValue:@"default_image.png" forKey:@"image"];
}