Skip navigation.
 
mlRe: Overriding -copyWithZone: the right way
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

Related mailsAuthorDate
mlOverriding -copyWithZone: the right way Michael Becker Nov 5, 17:46
mlRe: Overriding -copyWithZone: the right way Jonathon Mah Nov 5, 19:24
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 5, 19:31
mlRe: Overriding -copyWithZone: the right way Jean-Olivier Lanct… Nov 5, 19:52
mlRe: Overriding -copyWithZone: the right way Bill Garrison Nov 5, 22:42
mlRe: Overriding -copyWithZone: the right way Evan Schoenberg Nov 5, 22:50
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 5, 23:09
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 5, 23:24
mlRe: Overriding -copyWithZone: the right way Jean-Olivier Lanct… Nov 6, 00:27
mlRe: Overriding -copyWithZone: the right way Byron Wright Nov 6, 00:39
mlRe: Overriding -copyWithZone: the right way Jean-Olivier Lanct… Nov 6, 01:12
mlRe: Overriding -copyWithZone: the right way Steven Kramer Nov 6, 08:52
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 6, 16:50
mlRe: Overriding -copyWithZone: the right way The Karl Adam Nov 9, 02:04
mlRe: Overriding -copyWithZone: the right way The Karl Adam Nov 9, 02:24
mlRe: Overriding -copyWithZone: the right way M. Uli Kusterer Nov 9, 14:38