0

我有一个NSMutableDictionary这样初始化的:

dictionary = [NSMutableDictionary dictionaryWithDictionary:@{
    @"0": @{
        @"title": @"Description",
        @"value": @"Enter Description",
    },
    @"1": @{
        @"title": @"Amount",
        @"value": @"Enter Amount",
    }
}];

现在我想修改@"Enter Description"我一直在尝试这样做的内容:

NSMutableDictionary *tempDictionary = [dictionary objectForKey:@"0"];
[tempDictionary setObject:@"TEST" forKey:@"value"];
//The next line doesn't get executed yet since the error occurs at the line above.
//So Maybe there is something wrong with the one below as well ;-)
[dictionary setObject:tempDictionary forKey:@"0"];

但是我收到以下错误消息:

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSDictionaryI setObject:forKey:]: 无法识别的选择器发送到实例

我发现不知何故我的NSMutableDictionary一定已经变成了一个NSDictionary,这就是为什么我不能编辑它。也许您可以帮助我解决这个问题并为我指明正确的方向,或者向我展示我可以找到答案的资源?

4

1 回答 1

1

您的“子词典”是不可变的。

尝试这个:

dictionary = [NSMutableDictionary dictionaryWithDictionary:@{
                                                             @"0": [NSMutableDictionary dictionaryWithDictionary:@{
                                                                     @"title": @"Description",
                                                                     @"value": @"Enter Description",
                                                                     }],
                                                             @"1": [NSMutableDictionary dictionaryWithDictionary:@{
                                                                     @"title": @"Amount",
                                                                     @"value": @"Enter Amount",
                                                                     }]
                                                             }];
于 2015-08-19T21:15:47.353 回答