Re: On more newbie question about init...
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.