• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Overriding -copyWithZone: the right way
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Overriding -copyWithZone: the right way


  • Subject: Re: Overriding -copyWithZone: the right way
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Fri, 5 Nov 2004 23:24:40 +0100

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
_______________________________________________
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


References: 
 >Overriding -copyWithZone: the right way (From: Michael Becker <email@hidden>)
 >Re: Overriding -copyWithZone: the right way (From: Jean-Olivier Lanctôt <email@hidden>)
 >Re: Overriding -copyWithZone: the right way (From: Evan Schoenberg <email@hidden>)

  • Prev by Date: Re: Overriding -copyWithZone: the right way
  • Next by Date: Making a valid NSURL that contains Kanji characters
  • Previous by thread: Re: Overriding -copyWithZone: the right way
  • Next by thread: Re: Overriding -copyWithZone: the right way
  • Index(es):
    • Date
    • Thread