iphone - 如何更新 NSMutableDictionary

标签 iphone xcode4 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'

我们如何更新字典?

最佳答案

即使我的字典是可变的,我之前也遇到过同样的异常。
让我向您解释一下我的情况,也许会有帮助:
我有 NSMutableArrayNSMutableDictionary

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/

相关文章:

iphone - 适用于 BlackBerry 和 iPhone 的测试自动化工具

macos - 你的第一个 Mac 应用程序——编译器看不到 updateUserInterface

ios - 将 NSMutableDictionary 添加到 NSMutableDictionary 是否制作副本或保留指针

ios - 删除 NSMutableArray 内的 NSMutableDictionary 内的 <null>

iphone - 使用控件名称作为字符串转换为控件

ios - 如何在google中获取用户出生日期和性别加上ios中最新的SDK版本2.1.0

iphone - 添加超时?

ios - 单击按钮时将数据存储在 NSMutable 数组中

ios - 如何在 iOS 模拟器中模拟本地/推送通知?

objective-c - 如何在 Xcode 4 中为我的产品设置环境变量?