Re: copy vs mutableCopy?
Re: copy vs mutableCopy?
- Subject: Re: copy vs mutableCopy?
- From: Keary Suska <email@hidden>
- Date: Tue, 11 Jul 2006 10:53:29 -0600
- Thread-topic: copy vs mutableCopy?
on 7/11/06 10:07 AM, email@hidden purportedly said:
> As a followup, say I have an object with one instance variable, an
> NSMutableString. And say some code calls -copy on this object. You're
> saying that the intent is for an immutable copy?
The docs are not specific about this situation, but this is my
understanding.
> So I just retain the ivar and copy the reference to a new object and return
> that instance, even though the ivar is itself mutable? As the implementor of
> the - copy method, I am trusting that the client will not make changes to
> this value since it would affect another instance? Is this correct?
What you return should be a copy--i.e. a completely new and different
instance. The caller should trust that any changes made to the copy are not
reflected in the original. You should *never* retain a current object and
return it. If you do this you aren't making a copy.
This leads to shallow vs deep copying. Regarding your NSMutableString ivar,
whether or not you copy it depends on the "ownership" of the object.
Generally, I follow the general rule that if there is a set* accessor for
the ivar, I will use the method the accessor uses. E.g. If it retains the
passed object, I will retain when copying. If it copies the object, I will
copy when copying. Otherwise (i.e. no accessor), I generally make a copy of
objects. There are of course many other considerations depending on the use
of the object, but I find these general rules safe for most situations.
> And similarly, if -mutableCopy is called, I would make a deep copy of
> the NSMutableString, even though it's already mutable, because the
> intent of the caller is to modify that value and I don't want the
> original object mutated? Is this also correct?
Following the deep vs shallow above, keep in mind that -copy behavior
applies only to the object as a whole, and not necessarily to the individual
ivars. How you handle the ivars depends on how the object as a whole relates
to them.
Take for instance an NSArray. If one of its elements is an NSMutableString,
you can change that string at will even though the NSArray is immutable. You
could not, however, remove the element or change it to a different object.
Also, if you copy the NSArray, the mutable object elements do not suddenly
become immutable. This is because the NSArray doesn't "own" its element
objects, and so it simply retains them when added.
On the other hand, an NSDictionary must "own" its key objects, so it copies
them. My understanding is that if you copy an NSDictionary, you also copy
all of its key objects. The value objects, however, follow the same rules as
NSArray.
Hope this helps,
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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