• 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: Accessors Question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Accessors Question


  • Subject: Re: Accessors Question
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Thu, 12 Jun 2008 19:31:14 +0200


Le 12 juin 08 à 19:21, Jeff LaMarche a écrit :

Lately, I've started to see accessors of the following sort:

- (NSString *)foo
{
	return [[foo retain] autorelease];
}

rather than just

- (NSString *)foo
{
	return foo;
}

What is the purpose or benefit of doing this? It seems to me that this would add things unnecessarily to the autorelease pool, and I can't see a benefit to be had. I've seen it in places that make me think there must be a reason (e.g. sample code from Apple), so I'm guessing I'm missing something. Is there some benefit due to GC? If so, should this construct be used without GC or only with?

Thanks,
Jeff

I think the purpose is describe in the "writing accessor" guide :

 http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAccessorMethods.html

"As with values manufactured by class convenience methods, the returned object is autoreleased in the current scope and thus remains valid if the property value is changed.”

This is mainly to prevent this kind of issue:

NSString *foo = [myObject foo];
[myObject setFoo:nil]; // setter release the foo ivar => foo is no longer pointing on a valid memory location.




Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please 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

  • Follow-Ups:
    • Re: Accessors Question
      • From: Andy Lee <email@hidden>
References: 
 >Accessors Question (From: Jeff LaMarche <email@hidden>)

  • Prev by Date: Simple question - Subclassing NSView
  • Next by Date: Re: Simple question - Subclassing NSView
  • Previous by thread: Accessors Question
  • Next by thread: Re: Accessors Question
  • Index(es):
    • Date
    • Thread