Re: Mutability
Re: Mutability
- Subject: Re: Mutability
- From: email@hidden
- Date: Wed, 21 Nov 2001 22:01:21 +0100
At 16:03 Uhr -0500 20.11.2001, Peter Ammon wrote:
On Monday, November 19, 2001, at 01:07 PM, Norbert Heger wrote:
Fortunately we now have real immutable collection classes (in 10.0 all
collection classes were mutable). Copying an immutable instance now
simply
retains and returns the very same object, and invoking a mutating method
raises an exception.
But be aware of the fact, that even if an instance is immutable, it
will be
a subclass of a mutable (abstract) superclass. Thus invoking [array
isKindOfClass:[NSMutableArray class]] will NOT tell you if the given
array
is in fact mutable. Instead it always returns YES.
[...]
Thanks for the heads up, I'm sure this would have bitten me sooner
or later. So what's the proper way to determine if a collection is
mutable or not, if we can't use isKindOfClass: ?
I had exactly this problem some time ago. The answer is: there is
*no* way (at least none that is not dependant on exact knowledge of
the "hidden" classes in the class cluster).
There is a way: try to change the collection (in a way you can undo ;-),
by adding a new item for instance. If the collection is immutable, an
exception will be raised. So NS_HANDLER etc. will do the trick: if
exception -> immutable, if not -> mutable (and remember to restore to
previous state). You can even do that in a category of NSArray if you like.
Note that it works only in 10.1.
Thomas Lachand-Robert
********************** email@hidden
<< Et le chemin est long du projet ` la chose. >> Molihre, Tartuffe.