4

So far I've seen it done three ways:

1:

- (instancetype)init
{
    self = [super init];
    if (self) {
        // ...
    }
    return self;
}

2:

- (instancetype)init
{
    if (self = [super init]) {
        // ...
    }
    return self;
}

3:

- (instancetype)init
{
    if ((self = [super init]) == nil)
        return nil;
    // ...
    return self;
}

Which form is more idiomatic Objective-C?

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
Robert Atkins
  • 23,528
  • 15
  • 68
  • 97
  • Please cite supporting references from Apple/NextStep/Objective-C documentation or other recognized authorities, I'm not really looking for J. Random Developer's opinions here. – Robert Atkins Mar 27 '13 at 09:41
  • Check http://stackoverflow.com/questions/2956943/why-should-i-call-self-super-init for a good explanation. Its the situation and the developer that has to make the choice. – Rakesh Mar 27 '13 at 10:11
  • 2
    Number 3 needs to have a value for the early return. – jscs Mar 27 '13 at 16:55
  • I knew this was a "closure risk" when I wrote it, but I would contend it's the answers so far which are not constructive, rather than anything inherent in the question. – Robert Atkins Mar 28 '13 at 10:44
  • I voted to reopen as with over 1000 views it's now a "popular question", hence useful, and the accepted answer cites valid references (modern and historical Xcode defaults). – Robert Atkins Feb 06 '14 at 11:18

4 Answers4

5
  1. Most 'modern' approach (used by default in Xcode 4).

    - (instancetype)init
    {
        self = [super init];
        if (self) {
            // Initialization code here.
        }
        return self;
    }
    
  2. The default in older versions of Xcode.

    - (instancetype)init
    {
        if (self = [super init])
        {
            // Initialization code here.
        }
        return self;
    }
    
  3. While 'legal' it is very rare to see, and I wouldn't recommend it.

Traditionally in Objective-C 1.0 the init methods returned id, as of more recent iterations of Objective-C 2.0 it is recommended to return instancetype instead.

Atlas
  • 236
  • 1
  • 11
  • objC 2.0 has for a long time also used id for this purpose. The introduction of instancetype is a relatively recent event compared to the age of objC 2.0. – Kaiserludi May 07 '15 at 13:40
2

All are the same, it doesn't matter. I recommend using one of the first two though. The last one is rarely done.

You probably won't find any recommendation on this level from Apple. They state, that you should set the super's init's return value to self. How you do that is up to you.

Implementing an Initializer

DrummerB
  • 39,814
  • 12
  • 105
  • 142
-1

all of three method are correct we usually do like that.

- (id) init
{
    self = [super init];

    if (self != nil)
    {
        // your code here
    }

    return self;
}
-1

first or second is preferred. No need to check whether it is nil, because "if" condition will automatically check that. Following is the standard way to use init.

- (id)init
{
    self = [super init];
    if (self) {
        <#initializations#>
    }
    return self;
}
Vinoth Kumar
  • 156
  • 8