Re: Re: copy vs mutableCopy?
Re: Re: copy vs mutableCopy?
- Subject: Re: Re: copy vs mutableCopy?
- From: "Michael Ash" <email@hidden>
- Date: Tue, 11 Jul 2006 13:17:53 -0400
On 7/11/06, Rob Ross <email@hidden> wrote:
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? 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?
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?
-copy versus -mutableCopy only makes sense if you have mutable and
immutable variants of the same class. For example, NSSet versus
NSMutableSet. It doesn't sound like you have two variants of your
class, you just have the one. In this case, -copy versus -mutableCopy
makes no sense here. Implement -copyWithZone: to make a copy; if your
class is mutable, then it should make a true mutable copy, and if your
class is immutable then you can optimize it by just retaining.
Since most user classes don't distinguish between mutable and
immutable (they're almost always all mutable, or all immutable),
-mutableCopyWithZone: is something you almost never have to implement.
Mike
_______________________________________________
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