如何更新NSMutableDictionary

alr*_*rea 2 iphone nsmutabledictionary xcode4

我有

NSMutableDictionary *mutDic;
Run Code Online (Sandbox Code Playgroud)

NSMutableDictionary 从一个警报中加载了一些来自我试图更新其值的警报值

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

但我得到了这个例外

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
Run Code Online (Sandbox Code Playgroud)

我们如何更新字典?

Ank*_*kur 5

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

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [array objectAtIndex:0];
Run Code Online (Sandbox Code Playgroud)

[dict setObject:@"" forKey:@""]; < - 它在这条线上崩溃了......

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

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[array objectAtIndex:0]];
Run Code Online (Sandbox Code Playgroud)

它工作得很好:)