0

I just stumbled this code here on Stack Overflow:

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

Upon asking why we use this, and after searching via Google, I found that this is a classic way of doing it.

However I never saw ugh code when writing applications. Is this still used in Objective-C 2.0?

jscs
  • 63,694
  • 13
  • 151
  • 195
TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • 1
    what do you mean by "ugh code"? – Michael Dautermann Nov 21 '11 at 09:34
  • Oh, and your question [may have a duplicate here](http://stackoverflow.com/questions/6482635/init-in-automatic-reference-counting). – Michael Dautermann Nov 21 '11 at 09:36
  • 1
    Please update you question to show the init method style you are used to using, then answerers will be able to compare the two for you. – jrturton Nov 21 '11 at 09:36
  • Agreed with @jrturton. It's not clear what your particular concern is with this code ("classic way of doing" _what_?), and the question won't have much relevance to a future reader. – jscs Nov 21 '11 at 21:24

1 Answers1

4

It certainly is the common pattern for construction of new objects. What should have changed? The super class' init method may fail, and so may your part of the initialization, in which case you would not return self, but nil.

Daniel Schneller
  • 13,728
  • 5
  • 43
  • 72