Reputation: 5316
I tried to manipulate an array in an NSMutableDictionary
directly:
[[myDictionary objectForKey: @"key"] addObject: object];
This doesn't work! I even tried type casting:
[(NSMutableArray*)[myDictionary objectForKey: @"key"] addObject: object];
This didn't work either!
The only way that worked was:
NSMutableArray *array = [myDictionary objectForKey: @"key"];
[array addObject: object];
[myDictionary setObject: array forKey: @"key"]
Is there a way to manipulate the array in the dictionary similar to the first code snippet; i.e. without having to create a new array, manipulating it and then saving it?
Upvotes: 1
Views: 319
Reputation: 5316
The problem was that I wasn't allocation the array within the NSMutable Dictionary i.e. before adding objects use the following code:
[myDictionary addObject: [[NSMutableArray alloc]init] forKey: @"key"];
Upvotes: 1
Reputation: 22701
Are you loading the NSDictionary from a file or URL by any chance? From the API docs: "The objects contained by this dictionary are immutable, even if the dictionary is mutable."
Upvotes: 1