On Jul 26, 2013, at 5:15 PM, Keith Knauber wrote:

> From Apple Docs:
> Values returned from NSUserDefaults are immutable, even if you set a mutable 
> object as the value. For example, if you set a mutable string as the value 
> for "MyStringDefault", the string you later retrieve usingstringForKey: will 
> be immutable.
> 
> 
> The Apple docs have always said this.  In practice, dictionaries and arrays 
> have always been mutable,
> despite Apple's warning, as long as you used synchronize.
> The difference in Mountain Lion is that now, if you read/write a deeply 
> nested dictionary, those deeply nested objects are not saved to 
> NSUserDefaults.

The moral of the story is, don't rely on undocumented side-effects ;-) It is 
more likely that this behavior is a bug that has since been fixed. It would be 
a bug, as the user defaults system *should* know when its values change, but is 
unable to. The synchronize trick simply forces it to write out its values. I 
wouldn't be surprised of user defaults now makes immutable copies of nested 
structures, which is why changing your own structures does not make any changes 
to the defaults system.

> They may even look like they've been saved because you can read the values 
> back right before you exit your app.
> Subtly, they are not there when you relaunch.

They likely aren't there to begin with, because you aren't querying the user 
defaults when you output values, you are querying your own data structures. You 
will probably find a different behavior if you query NSUserDefaults directly.

> Even worse, making a mutableCopy doesn't solve the problem.
> Only making a mutableCopyDeepPropertyList solves the problem (see solution 
> below)

Although this is what you need to do to to change the deep structures, you 
would still need to re-set the top level default with the whole structure. If I 
were in your shoes, and needed complex nested structures in defaults, I would 
not use NSUserDefaults at all but instead have a Core Data store. Although it 
takes a little more up-front prep time, you get a whole lot more bang for your 
buck....

You might consider filing a radar for nested structure support in 
NSUserDefaults.

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to