Re: -(id)init methods, NSExceptions, and returning nil
Re: -(id)init methods, NSExceptions, and returning nil
- Subject: Re: -(id)init methods, NSExceptions, and returning nil
- From: Roland King <email@hidden>
- Date: Tue, 28 Jun 2011 00:49:34 +0800
yes I know it's equivalent, didn't say it wasn't, I said it was the favored form, which I remembered from Scott's message a year ago in this thread
http://www.cocoabuilder.com/archive/cocoa/286849-self-super-init-nil.html#286952
where he said
This has been, or is being updated.
The suggested pattern is now
- (id)init
{
self = [super init];
if (self)
{
}
return self;
}
All our documentation has been updated to reflect this (even if it hasn’t necessarily made it out to the users yet)
ok he said 'suggested', I didn't look up the message until now. I believe that's the version all the templates give you and my fingers have eventually started doing it this way too.
Nobody ever did answer the question why Apple prefers that form though, or if they did, I sure don't remember the answer.
On 28-Jun-2011, at 12:30 AM, Kyle Sluder wrote:
> On Mon, Jun 27, 2011 at 6:38 AM, Roland King <email@hidden> wrote:
>>> The favored form for writing an init method seems to be
>>>
>>> -(id)init
>>> {
>>> if (self = [super init])
>>> {
>>> // Do something here
>>> }
>>> return self;
>>> }
>>
>> actually I think the current favoured method is
>>
>> -(id)init
>> {
>> self = [ super init ];
>>
>> if( self )
>> {
>> // your code here
>> }
>>
>> return self;
>> };
>
> These are equivalent. All you did was move the self assignment out of
> the if statement.
>
> I actually prefer to use `if (!(self = [super init])) return nil;`.
> Again, it is equivalent.
>
> --Kyle Sluder
_______________________________________________
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