• 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: On more newbie question about init...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: On more newbie question about init...


  • Subject: Re: On more newbie question about init...
  • From: Wade Tregaskis <email@hidden>
  • Date: Sat, 14 Jun 2003 13:43:17 +1000

-(id)initWithFileContents:(NSString*) filePath
{
if (self = [super initWithFileContents:filePath])
{
image = [[NSImage alloc] initWithContentsOfFile:filePath];

if (image == nil)
{
[self release];
return nil;
}
}

return self;
}

Is it OK to call release on self like this to fail out of the imbedded object doesn't create?

Of course. If you're coming from a C++/Java background, remember to make the conscious observation that ObjC doesn't frown upon using self. In fact, it's generally required to write standard initializers, like this.

The assumption made when an initializer returns nil is that it has called release on the receiver. This saves people using the class having to check explicitly for nil (although arguably they always should) and call release themselves.

The only thing I personally would change is to say "self = nil" rather than "return nil", because you then have a single exit point function, which is supposedly good programming practice, but more importantly (to me) so that it's consistent with "self = [super ...]". But this is entirely personal preference, and of course makes no functional difference.

Wade Tregaskis
-- Sed quis custodiet ipsos custodes?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >On more newbie question about init... (From: Scott Andrew <email@hidden>)

  • Prev by Date: Re: On more newbie question about init...
  • Next by Date: Re: NSApplication delegate not released on quit?
  • Previous by thread: Re: On more newbie question about init...
  • Next by thread: Cocoa's custom class delegate conventions
  • Index(es):
    • Date
    • Thread