Re: Looking at self = [super init].
Re: Looking at self = [super init].
- Subject: Re: Looking at self = [super init].
- From: Jean-Daniel Dupas <email@hidden>
- Date: Fri, 29 May 2015 19:40:58 +0200
> Le 29 mai 2015 à 19:22, Alex Zavatone <email@hidden> a écrit :
>
> Was just looking at good old object initialization and came across a stupid idea.
>
> For most object initialization, we do this:
>
> - (id)init {
> if (self = [super init]) {
> // Set up stuff here.
> // this could get long.
> }
> return self;
> }
>
> in some cases, the set up within the parens could get pretty long. In cases like that, I generally set up another method to handle that for organization, but if you're passing objects into into your init method, then you're passing more data again and the code could get less cleaner looking than it could be.
>
> So, I thought, "why don't we check if self != [super init] and then immediately return if that is the case?"
>
> That would change object initialization to this:
>
> - (id)init {
> if (self != [super init]) {
> return;
> }
>
> // Set up stuff here.
> // We don't care if this gets long.
> // We really don't. This could go on and on.
>
> return self;
> }
>
And now that a reread the code, it is patently wrong.
if (self = [super init]) is a assignment to self not a comparison.
If you want to do a fast path, you have to do
self = [super init];
if (!self)
return nil;
_______________________________________________
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