我有
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'
我们如何更新字典?
最佳答案
即使我的字典是可变的,我之前也遇到过同样的异常。
让我向您解释一下我的情况,也许会有帮助:
我有 NSMutableArray
的 NSMutableDictionary
,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
dict = [array objectAtIndex:0];
[dict setObject:@""forKey:@""];
<-- 它在这一行崩溃了...
所以我改变了我的代码如下,
NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:[array objectAtIndex:0]];
效果很好:)
关于iphone - 如何更新 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9157190/