Re: Determining if NS/CFDictionary is mutable?
Re: Determining if NS/CFDictionary is mutable?
- Subject: Re: Determining if NS/CFDictionary is mutable?
- From: Douglas Davidson <email@hidden>
- Date: Mon, 23 Dec 2002 17:26:43 -0800
On Monday, December 23, 2002, at 10:52 AM, Nicholas Riley wrote:
Is there any public, documented way I can figure out whether a
NSDictionary is a mutable dictionary?
...
Without being able to do this, I end up creating lots of autoreleased
dictionaries because my API takes a NSDictionary and outputs a
NSDictionary. 99% of the time it will be mutable, so I'd like to save
the object creation overhead where I can.
When a method states that it returns an NSDictionary rather than an
NSMutableDictionary, it's not stating that the dictionary returned is
in fact immutable. Rather, it's stating that the dictionary should not
be mutated by the caller. Likewise, when a method states that it takes
an NSDictionary rather than an NSMutableDictionary, it's stating that
it will not mutate the dictionary. You can think of this as being like
a "const" keyword.
If you receive a dictionary, you can't just look at it and decide that
it's mutable, therefore it's OK to mutate it. Whoever sent it to you
has a right to expect that it will not be mutated, if it was returned
via a method returning NSDictionary, or passed in to a method taking
NSDictionary.
One thing you can do is to supply a mutable dictionary variant of your
method--for example, - (void)adjustDictionary:(NSMutableDictionary
*)dictionary vs. - (NSDictionary *)adjustedDictionary:(NSDictionary
*)dictionary. The caller should know whether it has a dictionary that
it is legitimate for you to mutate or not, and so can choose which
variant to use.
Douglas Davidson
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.