Re: exception in init
Re: exception in init
- Subject: Re: exception in init
- From: Dave <email@hidden>
- Date: Wed, 04 Apr 2012 18:39:16 +0100
Hi,
If it's a real error, this is what I do:
- (id) initWithURL: (NSURL *) url
{
self = [super init];
if (self == nil)
return nil;
if (url == nil)
{
[self release];
return nil;
}
return self;
}
or in this case, you could just do this:
- (id) initWithURL: (NSURL *) url
{
if (url == nil)
return nil;
self = [super init];
if (self == nil)
return nil;
return self;
}
The caller should check for a nil value returned from the init method.
If it's a programming error, e.g. it should never happen, then it's
probably best to log the error and abort the App.
Hope this helps.
All the Best
Dave
On 4 Apr 2012, at 12:38, Ariel Feinerman wrote:
Hi,
I think the question was asked and answered but I cannot see a
clearance
what is the right way to write init in the cases the arguments are
nil or
wrong?
Returning a nil or raising an exception?
- (id) initWithURL: (NSURL *) url {
if ((self = [super init])) {
if (!url) {
// ?
}
if (![url isFileURL]) {
// ?
}
}
return self;
}
I can frequently see the following in init
NSAssert(url, @"url may not be nil", );
--
best regards
Ariel
_______________________________________________
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:
40looktowindward.com
This email sent to email@hidden
_______________________________________________
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