FROM : M. Uli Kusterer
DATE : Fri Nov 05 23:24:40 2004
At 15:50 Uhr -0600 05.11.2004, Evan Schoenberg wrote:
>If the superclass supports copyWithZone and you therefore use [super
>copyWithZone:zone], keep in mind that your instance variables will
>be 'lightly' (not sure the proper word) copied - they point to the
>same memory addresses but are NOT retained.
"Shallow copy" is what you're fishing for.
>You therefore want to manually set all of your subclass's instasnce
>variables, like so:
>
>//Copy
>- (id)copyWithZone:(NSZone *)zone
>{
> MyClass *newCell = [super copyWithZone:zone];
>
> /* Font is a retained NSFont* */
> [newCell setFont:font];
>
> /* subString is a retained NSString* */
> [newCell setSubString:subString];
> return(newCell);
>}
No! If your setFont: looks like the typical one:
-(void) setFont: (NSFont*)f
{
if( font != f )
{
[font release];
font = [f retain];
}
}
This will simply do nothing, and thus not retain the instance
variable. Or if you're using the other variant:
-(void) setFont: (NSFont*)f
{
[font autorelease];
font = [f retain];
}
This will release an object that was never retained by you, thus
effectively also failing to do the additional "retain" needed to give
your copy ownership of the NSFont. Apple recommends to do:
-(id) copyWithZone: (NSZone*)zone
{
MyClass *newCell = [super copyWithZone: zone];
newCell->font = nil;
[newCell setFont: font];
newCell->subString = nil;
[newCell setSubString: subString];
return newCell;
}
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
DATE : Fri Nov 05 23:24:40 2004
At 15:50 Uhr -0600 05.11.2004, Evan Schoenberg wrote:
>If the superclass supports copyWithZone and you therefore use [super
>copyWithZone:zone], keep in mind that your instance variables will
>be 'lightly' (not sure the proper word) copied - they point to the
>same memory addresses but are NOT retained.
"Shallow copy" is what you're fishing for.
>You therefore want to manually set all of your subclass's instasnce
>variables, like so:
>
>//Copy
>- (id)copyWithZone:(NSZone *)zone
>{
> MyClass *newCell = [super copyWithZone:zone];
>
> /* Font is a retained NSFont* */
> [newCell setFont:font];
>
> /* subString is a retained NSString* */
> [newCell setSubString:subString];
> return(newCell);
>}
No! If your setFont: looks like the typical one:
-(void) setFont: (NSFont*)f
{
if( font != f )
{
[font release];
font = [f retain];
}
}
This will simply do nothing, and thus not retain the instance
variable. Or if you're using the other variant:
-(void) setFont: (NSFont*)f
{
[font autorelease];
font = [f retain];
}
This will release an object that was never retained by you, thus
effectively also failing to do the additional "retain" needed to give
your copy ownership of the NSFont. Apple recommends to do:
-(id) copyWithZone: (NSZone*)zone
{
MyClass *newCell = [super copyWithZone: zone];
newCell->font = nil;
[newCell setFont: font];
newCell->subString = nil;
[newCell setSubString: subString];
return newCell;
}
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de






Cocoa mail archive

