• 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: Convention for init with error
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Convention for init with error


  • Subject: Re: Convention for init with error
  • From: John McCall <email@hidden>
  • Date: Wed, 27 Feb 2013 13:26:35 -0800

On Feb 27, 2013, at 1:12 PM, Brad O'Hearne <email@hidden> wrote:
> 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?

Overall, I think that a factory method is a better design than tying this to allocation/initialization.

John.
_______________________________________________

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

References: 
 >Convention for init with error (From: "Brad O'Hearne" <email@hidden>)

  • Prev by Date: Convention for init with error
  • Next by Date: Re: Convention for init with error
  • Previous by thread: Convention for init with error
  • Next by thread: Re: Convention for init with error
  • Index(es):
    • Date
    • Thread