NSMutableDictionary 中的编辑条目失败,错误:"unrecognized selector sent to instance"



我有一个初始化的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,这就是为什么我不能编辑它。也许你可以帮我解决这个问题,给我指出正确的方向,或者告诉我可以找到答案的资源?

你的"子字典"是不可变的

试试这个:

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

相关内容

最新更新