Re: Looking at self = [super init].
Re: Looking at self = [super init].
- Subject: Re: Looking at self = [super init].
- From: Alex Zavatone <email@hidden>
- Date: Fri, 29 May 2015 13:38:10 -0400
On May 29, 2015, at 1:35 PM, Jean-Daniel Dupas wrote:
>
>> 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;
>> }
>
> You must at least returns nil in the fast path, else the compiler will not be happy.
>
DOH. Good catch. Sorry, was typing out by hand instead of copying and pasting. I'm actually returning nil in the real class.
So, that should be this:
- (id)init {
if (self != [super init]) {
return nil;
}
// Set up stuff here.
// We don't care if this gets long.
// We really don't. This could go on and on.
return self;
}
_______________________________________________
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