Re: Bug in NSArrayController? (immutable instead of mutable dictionaries)
Re: Bug in NSArrayController? (immutable instead of mutable dictionaries)
- Subject: Re: Bug in NSArrayController? (immutable instead of mutable dictionaries)
- From: Brent Gulanowski <email@hidden>
- Date: Fri, 22 Oct 2004 00:58:23 -0400
On Fri, 22 Oct 2004 04:10:29 +0200, M. Uli Kusterer
<email@hidden> wrote:
>
At 3:50 Uhr +0200 22.10.2004, Uli Zappe wrote:
>
>Well, it doesn't in that NSUserDefaultsController does *not* always
>
>return immutable objects;
>
>
However, it is *documented* as returning immutable objects. That's
>
all the guarantee you get. Mutable objects are a superset of
>
immutable objects, meaning everything an immutable object can do, a
>
mutable object can do as well.
That's a fact that tends to slip my consciousness regularly. It's the
disagreement between OOP and Boolean logic -- the incorrect idea that
mutable and immutable are negations of one another. In English, they
are so.
>
>
It's like when you buy a piece of wood, and they guarantee you that
>
you'll get at least 2 metres in length and 50cm in width. Just
>
because the last four pieces of wood you bought were actually 2
>
metres 20cm and 70cm in size it doesn't mean you can rely on that
>
always being the case. They made you a little present, that's all.
Maybe it is like that and maybe it isn't. Analogies get more people
into trouble than most any other figure of speech, except maybe for
gross over-generalization. ;-) The fact is that immutability is an
artificially enforced quality of an ADT. One has to make oneself
forget that the ADT actually encapsulates data (read: memory), which
by its nature is (usually) mutable. One tries to forget that the guts
exist, but nevertheless one believes that they are there.
>
>
In the case of NSUserDefaults, it's probably an internal
>
implementation detail, a performance optimization. Maybe the code
>
they use to load their prefs creates an empty, mutable array and
>
fills it with the objects as it reads from the preferences file.
>
Apple *could* copy this object, but that would be unnecessary
>
duplication. Since an NSMutableArray is a subclass of NSArray, and
>
user defaults are documented to return immutable objects (i.e. an
>
NSArray) even if the object originally stored was mutable, they're
>
violating none of the guarantees they gave you.
Wouldn't it just convert the string data using a decoder? I would have
thought it just calculates the size of a chunk of memory and writes it
out in one splat. Perhaps that would be unsafe.
>
>
>So the most important point IMHO is consistency of behavior. If it
>
>consistently returns immutable objects, I won't like it, but at
>
>least it would be consistent.
>
>
The behavior is consistent if you assume the documentation for
>
CFPreferences is right.
>
>
>Apart from that, wanting to store sets of preferences in an array
>
>and in the user defaults system at the same time is such an obvious
>
>application for Bindings that it should be possible to do this
>
>without all this hassle.
>
>
It is. Just treat the array as
>
>
>If Apple really decides to make immutable the consistent behavior,
>
>then at least they should provide a factory
>
>NSImmutableToMutableTransformer.
>
>
There already is. It's called "mutableCopy".
>
>
But IMHO you're going about this the wrong way. You should create
>
the array in your NSArrayController and synchronize it with the
>
prefs, not modify the prefs directly. If you have an intermediate
>
copy, all these problems shouldn't occur. And as I said "handles
>
objects as compound value" may help you get that problem resolved as
>
well.
>
Not sure what you mean by either "create the array in your
NSArrayController" or "modify prefs directly". The one could be
implying the use of compound value but then why repeat yourself? The
other suggestion I don't get at all. the NSArrayController retrieves
the objects from NSUserDefaultsController which itself retrieves them
from NSUserDefaults (I'm guessing). But in any case why is returned is
undoubtedly a copy.
What is still annoying is that while preferences are made to be
altered, their contents are not. But so it is written and thus it
shall be... Thus in order to not depend on undocumentated behaviour I
should have to add transformers wherever I use dictionaries or arrays
or other containers where (im)mutable is relevant. Not a lot of work
but an irksome additional complexity.
--
Brent Gulanowski
http://www.boredastronaut.com
_______________________________________________
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