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

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

  • Follow-Ups:
    • Re: Convention for init with error
      • From: Quincey Morris <email@hidden>
    • Re: Convention for init with error
      • From: John McCall <email@hidden>
  • Prev by Date: Re: archiving report
  • Next by Date: Re: Convention for init with error
  • Previous by thread: Re: 2 icons showing in dock
  • Next by thread: Re: Convention for init with error
  • Index(es):
    • Date
    • Thread