Convention for init with error
Convention for init with error
- Subject: Convention for init with error
- From: "Brad O'Hearne" <email@hidden>
- Date: Wed, 27 Feb 2013 14:12:05 -0700
Hello,
I have a need to construct an object with the possibility of an error taking place and needing to return that error. While it would be nice to have a clean init method that returned no errors, in this particular case, the error belongs with init. I've been pondering two ways of doing this:
1. Just a modified init method, where it returns nil for the return value and sets an error pointer like so:
- (id)init:(NSError **)error;
{
self = [super init];
if (self)
{
if ([self operationThatMightFail:error])
{
*error = nil;
}
else
{
return nil;
}
}
return self;
}
OR
2. Using a static method to perform the dirty work, and then encouraging the caller only to use this method to init the class, like so:
- (AThing *)athing:(NSError **)error;
{
AThing *a = [[AThing alloc] init];
if ([a operationThatMightFail:error])
{
*error = nil;
}
else
{
return nil;
}
return a;
}
What is the recognized convention or design pattern for addressing this in Objective C, or does it not really matter?
Thanks,
Brad
_______________________________________________
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