2012-02-06 52 views
2

如何更新的NSMutableDictionary

NSMutableDictionary *mutDic; 

其加载与其他NSMutableDictionary 从提示的一些值我试图更新它的值

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    [self.mutDic setValue:[[alertView textFieldAtIndex:0] text] forKey:@"lname"]; 
} 

,但我得到这个例外

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object' 

我们如何更新字典?

+0

崩溃日志说你的字典是不是可变初始化它。你能告诉我们你如何分配/初始化字典吗? – 2012-02-06 08:18:44

+0

如果您在@property中有'copy',则用'retain'或'strong'替换它。复制总是创建不可变的对象。 – 2012-02-06 08:26:22

回答

5

即使我的字典是可变的,我之前也有同样的例外。
让我解释一下我的情况给你,可能会有所帮助:
我有NSMutableDictionaryNSMutableArray

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
dict = [array objectAtIndex:0]; 

[dict setObject:@"" forKey:@""]; < - 这是崩溃的这条线......

所以我改变我的代码如下,

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[array objectAtIndex:0]]; 

它工作得很好:)

0

发现异常问题,但不能完全解决

负载我需要从其他字典取值为我所用的方法是错误的,我只是分配oldDic到mutDic,我改

self.mutDic = [[[NSMutableDictionary alloc] initWithDictionary:manager.oldDic] retain]; 

他们通过

self.oldDic = [[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"F Name",@"fname",@"L Name",@"lname", nil ]retain]; 

是解决异常