Re: [Q] Cocoa Object : Behavior when "retain" and "copy"?
Re: [Q] Cocoa Object : Behavior when "retain" and "copy"?
- Subject: Re: [Q] Cocoa Object : Behavior when "retain" and "copy"?
- From: Markus Magnuson <email@hidden>
- Date: Sun, 11 Feb 2007 18:40:48 +0100
11 feb 2007 kl. 17.18 skrev JongAm Park:
However, if it is "retained", although the method you call is
"copy", you should be aware that.. "Oh.. it can be retained,
although I call copy". Wouldn't programmers know when they want to
retain or shallow copy, or even deep copy? In my opinion, it would
be better if retain is retain, and copy is shallow copy in default.
If a programmer wants a deep copy, then he can extend the default
behavior and make it deep copy.
Well, the defined behaviour of the "copy" method in NSObject is to
retain immutable objects (and return the reference), or return an
immutable copy if the object is mutable. For example, you might have
this method (given that a "myString" ivar exists):
- (void)setString:(NSString *)newString
{
newString = [newString copy];
[myString release];
myString = newString;
}
You want an immutable string here, but because the passed string
could either be an NSString or an NSMutableString, you would want an
immutable version of it. So that is exactly what the "copy" method is
for.
I think it is the name of the method that confuses.
--
Markus Magnuson
email@hidden
"Life... is like a grapefruit. It's orange and squishy, and has a
few pips in it, and some folks have half a one for breakfast."
- Douglas Adams (1952 - 2001)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden