I’ll try to be as much clear as possible:
- I create 2 MutableDictionary
-
I add to both of them the same NSMutableArray object:
[self.myList setObject:tempC forKey:keyV]; [self.listFiltered setObject:tempC forKey:keyV]; -
In other part of the code, I want to empty one, so I do:
[self.listFiltered objectForKey:keyV] removeAllObjects];
The problem is that the objects are being removed in BOTH mutableDictionaries!
The same
NSNSMutableArrayis added to both dictionaries. It doesn’t matter whether you access it through one dictionary or the other, you end up manipulating the sameNSMutableArrayinstance.To fix this, you need store different
NSMutableArrayinstances in each dictionary. The easiest way to do this is through[NSMutableArray copy], which will do a shallow copy.Lastly, naming dictionaries with names ending in
listis a bad practice.