In the following code:
//anArray is a Array of Dictionary with 5 objs.
//here we init with the first
NSMutableDictionary *anMutableDict = [[NSMutableDictionary alloc] initWithDictionary:[anArray objectAtIndex:0]];
... use of anMutableDict ...
//then want to clear the MutableDict and assign the other dicts that was in the array of dicts
for (int i=1;i<5;i++) {
[anMutableDict removeAllObjects];
[anMutableDict initWithDictionary:[anArray objectAtIndex:i]];
}
Why this crash? How is the right way to clear an nsmutabledict and the assign a new dict?
Thanks guy’s.
Marcos.
You do not “reinit” objects — ever. Initialization is meant to be used on a newly
alloced instance and might make assumptions that aren’t true after initialization is complete. In the case of NSMutableDictionary, you can usesetDictionary:to completely replace the contents of the dictionary with a new dictionary oraddEntriesFromDictionary:to add the entries from another dictionary (without getting rid of the current entries unless there are conflicts).More generally, you could just release that dictionary and make a
mutableCopyof the dictionary in the array.