Edit entry in NSMutableDictionary fails with error: "unrecognized selector sent to instance"

Edit entry in NSMutableDictionary fails with error: "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"];

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

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance

我发现我的 NSMutableDictionary 一定以某种方式变成了 NSDictionary,这就是我无法编辑它的原因。也许你可以帮我解决这个问题并指出正确的方向,或者告诉我可以找到答案的资源?

您的 "sub dictionaries" 不可变。

试试这个:

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