Re: Copy and release
Re: Copy and release
- Subject: Re: Copy and release
- From: Wade Tregaskis <email@hidden>
- Date: Fri, 22 Oct 2004 23:35:01 +1000
If you make immutable copies of immutable NSString, NSDate, NSArray
or NSDictionary objects (to pick a few important examples) you simply
get the same object with the reference count incremented. This saves
some data copying for simple objects and by the time you get to
complex objects like dictionaries the cost of copying can get pretty
high.
A question I've had is when a container is said to be immutable, is it
still fair game to change the contents of the objects that are
contained?
For example: An NSArray containing NSMutableString objects, or an
NSData object. Can you / Should you change the contents of the
NSMutableStrings in the NSArray or the Memory block pointed to by the
NSData?
Should items contained in immultable container classes be considered
immutable?
Not necessarily. Your design should specify if this is the case,
otherwise it's most likely assumed that the immutability attribute
applies only to the container, not it's contents. This is consistent
with the rest of Foundation's container behaviour, and that of other
common containers such as found in the STL (where everything, most
notably copies, are shallow in so far as containers holding pointers
are concerned).
Having said all that, you really shouldn't assume anything about the
mutability of any class, but rather call the appropriate method
(retain, copy or mutableCopy), as I'm about to explain in answer to
your next question.
P.S. Sorry for taking this further off-topic. As far as the OP goes,
the main effeciency point I've seen when dealing with mutable Vs.
immutable is that a copy of an immutable object can just be a
reference to the same object, but a mutable copy needs to be a full
copy. Things is, why do you need a copy? If it's to change it, then it
needs to be mutable, and you need a full copy. If it's just to call it
by a different name, then regardless of wether it's a mutable object
or not you just need to pass a reference around.
Perhaps, but it's an issue of generality - your code needn't know
necessarily whether the object it's been given is mutable or not (since
it could be any subclass of your parameter type, and either way one or
the other is going to have to thus satisfy any dynamic typing
requirements you set, short of using isMemberOfClass), but it usually
knows what behaviour it wants from that object, whether mutability or
immutability. For example, a container allows it's contents to be
mutable, since that's a necessary behaviour - you don't want to have to
rebuild your container every time you change one item. But a socket
class, for example, would want an immutable copy of the original
address it's given to connect to, since you don't want it to change for
the life of that particular instance. So the container would retain
it's objects, the socket would copy it's parameter (which might boil
down to a retain, if the given NSString is immutable anyway; that's
just an efficiency thing, as mentioned by another poster).
From the converse point of view, if you want a mutable object because
you know you'll change it, you mightn't want to limit your callers to
providing a mutable version of the object... ultimately it's easier to
call mutableCopy once in your target method than in some, most or even
all your callers. As with the immutable case just mentioned, if the
given object is already of the correct mutability you'll just end up
retaining it.
Ultimately Foundation could survive without explicit mutability
conversion, but it would then require stronger typing, and ObjC is a
fantastic language for exactly the opposite reason.
Wade Tregaskis (AIM, Yahoo & Skype: wadetregaskis, ICQ: 40056898, MSN &
email: email@hidden, Jabber:
email@hidden
-- Sed quis custodiet ipsos custodes?
_______________________________________________
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