2017-08-11 43 views
0

我正在嘗試設置在NSMutableDictionary值,但我總是收到此錯誤:的iOS:不能設置爲空值的一個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

使用'快enumration' –

+6

你的字典裏是不變的 - 這是一個'NSDictionary'而不是'NSMutableDictionary'。欲瞭解更多信息,你可以從[這裏](https://stackoverflow.com/questions/14731353/nsdictionaryi-setobjectforkey-unrecognized-selector-sent-to-instance) –

+0

@ Anbu.Karthik檢查我的編輯請 –

回答

0

你的字典是不變 - 這是一個NSDictionary中,而不是一個的NSMutableDictionary。如果你要轉換的任何(數組,字典)不變可變使用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"]; 
}