Re: using NSMutableDictionary in place of NSDictionary
Re: using NSMutableDictionary in place of NSDictionary
- Subject: Re: using NSMutableDictionary in place of NSDictionary
- From: Greg Herlihy <email@hidden>
- Date: Fri, 24 Feb 2006 15:56:49 -0800
- Thread-topic: using NSMutableDictionary in place of NSDictionary
An NSMutableDictionary is an NSDictionary because NSMutableDictionary
inherits from NSDictionary. So wherever you can use an NSDictionary you are
free to substitute an NSMutableDictionary in its place. So the answer is no,
there is no conversion back to an NSDictionary required here.
Of course every NSDictionary object is not necessarily an
NSMutableDictionary as well. So substitution in the opposite direction is
not guaranteed: if a routine returns an NSDictionary for a result, you
cannot treat it as an NSMutableDictionary (even though it could actually be
one).
Greg
On 2/24/06 11:36 AM, "Steve Sheets" <email@hidden> wrote:
> You can safely say that we have recently discovered the joys of
> NSDictionary, and have seen the light. We are starting to use use
> them to handle static application data, user defaults and document
> data. We have noticed one consistent coding strategy, and want to
> hear some comments on this.
>
> Almost always, when working with persistent, non-static dictionaries
> (ie. ones that the program changes), we convert the NSDictionary into
> an NSMutableDictionary. Sometimes the dictionary contains sub-
> dictionaries, and we walk the tree, converting them as we go.
>
> When we want to save this information, do we have to convert this
> mutable dictionary back to the NSDictionary, or can we save save it
> as NSMutableDictionary? The two main sets of read/write calls we are
> referring to:
>
> To read/write to File:
>
> a_dictionary = [NSDictionary
> dictionaryWithContentsOfFile:a_file_name]; // read file
> [a_dictionary writeToFile:a_file_name atomically:YES]; // write file
>
> To read/write to Defaults
>
> a_dictionary = [[NSUserDeafaults standardUserDefaults]
> dictionaryForKey:a_key]; // read defaults
> [[NSUserDeafaults standardUserDefaults] setObject: a_dictionary
> forKey:a_key]; // write defaults
>
> We were concerned that some of this encoding/decoding would actually
> check what type of object was being written, and want explicitly an
> NSDictionary object. For example, if we save a NSMutableDictionary
> using User Defaults, would it return as NSDictionary or
> NSMutableDictionary? The actual data is stored as XML plist, so we
> thought it would be ok, but the documentation ALWAYS talks about
> using NSDictionary in the calls, never mentioning
> NSMutableDictionary. The same is true with writing dictionary to files.
>
> This question turned out to be more long winded that it needed to be,
> so simple answers would be fine with us!
>
> As always, thanks!
>
> Steve Sheets
> Midnight Mage Software
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden