Re: NSDictionary mutability test
Re: NSDictionary mutability test
- Subject: Re: NSDictionary mutability test
- From: Jonathan Hess <email@hidden>
- Date: Mon, 8 Dec 2008 12:18:25 -0800
On Dec 8, 2008, at 2:05 PM, I. Savant wrote:
On Mon, Dec 8, 2008 at 1:53 PM, Sherm Pendley
<email@hidden> wrote:
I was using this assertion (written some time ago in ignorance)
and wasted
an afternoon tracking down the fact that it didn't work.
It does "work" - it does the job it's designed to do.
I'll join the fray on this one. :-) I agree with you, Sherm, to a
point. It works (does what it's designed to do), but because of the
special case of class clusters, it'd be handy in some cases to be able
to ask an object if it -isMutable.
Maybe the only solution is to set up an exception handler.
No, the solution is to not try to test for mutability. Such tests
don't work
because they're not *supposed* to work - code that tries to do such
things
is broken as designed. And no, it's not an "ethical" issue. Writing
broken
code isn't a question of good and evil, it's simply a question of
what works
and doesn't work.
This is the most salient point. Either you (the developer) created
the dictionary or it was handed to you. If you created it, you should
know whether it is mutable. If you did not, the documentation (if it
exists) and the method signature should tell you all you need to know.
The only thing you could consider "broken" is if the method promises
an immutable object but gives you a mutable one (or vice-versa).
A method returning an NSDictionary * doesn't mean that the dictionary
won't mutate, it means the caller isn't allowed to mutate the returned
dictionary. If you want an immutable dictionary, you need to copy the
result. Many methods in cocoa hand out mutable collections through
pointers that are typed as the immutable variant. Without this, the
accessor would have to copy, and that would safe, but slow.
Jon Hess
If there's ever an ambiguous situation and you need to enforce one
or the other, make a -copy or -mutableCopy of the object to be sure,
and rid yourself of the worry.
--
I.S.
_______________________________________________
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
_______________________________________________
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