Re: verify input parameter of init method and return nil before invoke [super init]
Re: verify input parameter of init method and return nil before invoke [super init]
- Subject: Re: verify input parameter of init method and return nil before invoke [super init]
- From: Greg Parker <email@hidden>
- Date: Tue, 29 Jan 2013 23:25:26 -0800
On Jan 29, 2013, at 10:25 PM, Quincey Morris <email@hidden> wrote:
> However, it occurs to me there's a better solution. Since we're talking about ARC, this pattern:
>
> + (id) createBlaWithFoo: (NSString*) foo // or the return type can be Bla* if you want
> {
> if (!foo)
> return nil;
> else
> return [[Bla alloc] initWithFoo: foo];
> }
>
> is superior all round. It has no autorelease penalty, may reduce the number of keystrokes at the call site, and is impervious to the particular exploding dealloc problem we've been discussing.
Name the method `new...` instead of `create...`. Otherwise you do suffer an autorelease penalty with ARC. (`create...` is not one of the names that ARC assumes will return a retained result.)
Use `[self alloc]` instead of `[Bla alloc]`. Otherwise you won't work well with subclasses.
Return `(instancetype)` instead of `(id)` or `(Bla *)`. Otherwise you won't work well with subclasses. (The compiler will assume instancetype if your method is named `new...`, but not if you use some other name.)
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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