• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSUserDefaults and mutability
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSUserDefaults and mutability


  • Subject: Re: NSUserDefaults and mutability
  • From: Ricky Sharp <email@hidden>
  • Date: Sun, 9 Mar 2008 07:09:33 -0500


On Mar 9, 2008, at 5:58 AM, Trygve Inda wrote:


On 8 Mar '08, at 1:57 PM, Trygve Inda wrote:

Is this expected? Can I rely on it? I will never need to change
dict, but I
am modifying items within a known sub Dictionary.

Don't rely on this; it's entirely possible this behavior could change in the future, causing your app to throw an exception. Something much like that happened in 10.4 (I think) — maybe it was the mutability of collections read from property lists — and broke a bunch of apps.

—Jens

Is it enough then to take the dictionary I get back and do a [dict mutableCopy]?

Will this cascade down to the "subdicts" (dicts within the top level dict).


No it won't. mutableCopy only operates on one particular object and not all its contained objects. While that behavior does sound nice in some situations, you'd definitely not want that in all situations.

If you need to make modifications to various objects, make sure they are mutable.

NSDictionary* theImmutableDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:555] forKey:@"MyNumberOne"], @"MyDictionaryOne",
[NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:777] forKey:@"MyNumberTwo"], @"MyDictionaryTwo",
nil];

NSLog (@"theMutableDictionary BEFORE = %@", theImmutableDictionary);


NSDictionary* theMutableDictionary = [theImmutableDictionary mutableCopy];

//Legal
[theMutableDictionary setValue:[NSMutableDictionary dictionary]
forKey:@"MyDictionaryOne"];

//Illegal
//[[theMutableDictionary objectForKey:@"MyDictionaryTwo"]
// setObject:[NSNumber numberWithInt:333] forKey:@"MyNumberTwo"];


//Legal
NSDictionary* theMutableNestedDictionary =
[[theMutableDictionary objectForKey:@"MyDictionaryTwo"] mutableCopy];

[theMutableNestedDictionary setValue:[NSNumber numberWithInt:333]
forKey:@"MyNumberTwo"];
[theMutableDictionary setValue:theMutableNestedDictionary
forKey:@"MyDictionaryTwo"];

NSLog (@"theMutableDictionary AFTER = %@", theMutableDictionary);


___________________________________________________________
Ricky A. Sharp         mailto:email@hidden
Instant Interactive(tm)   http://www.instantinteractive.com

_______________________________________________

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


  • Follow-Ups:
    • Re: NSUserDefaults and mutability
      • From: Trygve Inda <email@hidden>
References: 
 >Re: NSUserDefaults and mutability (From: Trygve Inda <email@hidden>)

  • Prev by Date: How to get this bundle with identifier
  • Next by Date: Re: How to get this bundle with identifier
  • Previous by thread: Re: NSUserDefaults and mutability
  • Next by thread: Re: NSUserDefaults and mutability
  • Index(es):
    • Date
    • Thread