Reputation: 348
I have an NSMutableDictionary like this:
<dict>
<key>Dictionaries</key>
<dict>
<key>One</key>
<dict>
<key>Data</key>
<data>
qqqqqg==
</data>
</dict>
<key>Two</key>
<dict>
<key>Data</key>
<data>
qqqqqg==
</data>
</dict>
</dict>
</dict>
Which is essentially an [NSMutableDictionary]
that has other children dictionaries, all using the same number of objects and keys. The problem is that when I try and add another dictionary in the the existing 'Dictionaries' it wipes out all the others. I've tried using several functions including, setObject: forKey:
, setDictionary: forKey:
, addEntriesFromDictionary:
...
NSMutableDictionary* mainDictionary = [[NSMutableDictionary alloc] initWithDictionary:otherDictionary];
dictionaries = [mainDictionary objectForKey:@"Dictionaries"];
[dictionaries addEntriesFromDictionary:otherDictionary];
I must be doing something wrong, or not using the function correct. An example of how to append my dictionary with Three
, Four
, etc. would be very helpful. thanks.
Upvotes: 0
Views: 138
Reputation: 726987
When you make this call
NSMutableDictionary* mainDictionary = [[NSMutableDictionary alloc] initWithDictionary:otherDictionary];
the only mutable dictionary that you are guaranteed to get is the outer one (i.e. the mainDictionary
). If objects inside otherDictionary
are mutable, they would remain mutable; if the objects inside otherDictionary
are immutable, they would remain immutable.
It appears that the otherDictionary
that you are passing to initWithDictionary:
has been constructed from a property list. If this is the case, you can force all its containers to be mutable by passing NSPropertyListMutableContainers
to the propertyListWithData:options:format:error:
method, like this:
NSMutableDictionary *otherDictionary = [NSPropertyListSerialization
propertyListWithData:myData
options:NSPropertyListMutableContainers
format:NULL
error:NULL
];
Upvotes: 1