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 (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to