• 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: using NSMutableDictionary in place of NSDictionary
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: using NSMutableDictionary in place of NSDictionary


  • Subject: Re: using NSMutableDictionary in place of NSDictionary
  • From: Adam Leonard <email@hidden>
  • Date: Fri, 24 Feb 2006 23:38:41 -0800

As Greg mentioned, you will usually get a immutable dictionary when doing the things you are talking about, and they cannot be treated as mutable. However, most (if not all) cocoa immutable classes with mutable subclasses conform to the NSMutableCopying protocol, so to get a mutable object, all you need to do is call "mutableCopy".


On Feb 24, 2006, at 3:56 PM, Greg Herlihy wrote:

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


_______________________________________________ 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
References: 
 >Re: using NSMutableDictionary in place of NSDictionary (From: Greg Herlihy <email@hidden>)

  • Prev by Date: Re: Mutable/non-mutable bindings in NSTextView
  • Next by Date: NSUnarchiver: How to deal with locked file?
  • Previous by thread: Re: using NSMutableDictionary in place of NSDictionary
  • Next by thread: Re: using NSMutableDictionary in place of NSDictionary
  • Index(es):
    • Date
    • Thread