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

275 Views Asked by At

I have a NSMutableDictionary that is initialized like this:

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

Now I'd like to modify @"Enter Description" which I've been trying to do like this:

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"];

However I'm getting the following error message:

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

What I've found is that somehow my NSMutableDictionary must have gotten transformed into an NSDictionary, and this is why I can't edit it. Maybe you can help me figure this one out and point me into the right direction or show me the resources where I can find the answer?

1

There are 1 best solutions below

1
On BEST ANSWER

Your "sub dictionaries" are not mutable.

try this:

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