Mutating message sent to immutable object
Mutating message sent to immutable object
- Subject: Mutating message sent to immutable object
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sun, 01 Sep 2013 09:10:59 +0700
I have an NSObjectController called objectController with content bound to locationDictionary.
@property (strong) NSMutableDictionary *locationDictionary;
And an NSArrayController called arrayController which has its content bound to objectController.myArrayKey.
This array contains column-dictionaries.
And an NSTableView (non View-based) whose columns are bound to arrayController. The keys in the column-dictionaries of course correspond to the identifiers of the columns and each element in objectController.myArrayKey represents one row.
locationDictionary is set from standardUserDefaults
NSDictionary *a = [ standardUserDefaults dictionaryForKey: kToolbarLocations ];
NSDictionary *b = a[ someIdentifier];
self.locationDictionary = b;
But when editing the tableView this (sometimes, not always) crashed.
So I changed the last line to:
self.locationDictionary = [b mutableCopy];
This worked better, but still the occasional crash: "Mutating message sent to immutable object"; the message being: setObject:forKey: (obviously trying to change one of my column-dictionaries)
So I changed the line again to:
self.locationDictionary = [b veryDeepMutableCopy];
which converts NSArrays to mutable, and the elements thereof into mutable dictionaries.
Now I don't see any crashes anymore.
Two questions:
1. Is there a better way to do this (letting NSControllers change the stuff to mutable automagically)?
2. Why did this even occasionally work before I made my veryDeepMutableCopy?
NSUserDefaults tells me about dictionaryForKey: "The returned dictionary and its contents are immutable, even if the values you originally set were mutable."
Does this really mean: "The returned dictionary and its contents are mutable or immutable depending on our whims. You better not rely on their mutability state."
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden