Does it make sense to write self = [super init];
in a custom initialisation method while subclassing NSObject? I know it's necessary when subclassing any other class, because it might have a custom initialisation, but does the NSObject init method do anything?
Asked
Active
Viewed 1,655 times
5

Tim Vermeulen
- 12,352
- 9
- 44
- 63
-
4Its a good habit to get into. – Kyle Sep 14 '12 at 16:27
1 Answers
9
An object isn’t ready to be used until it has been initialized. The init
method defined in the NSObject
class does no initialization; it simply returns self.
So basically you don't necessarily have to call [super init]
in an NSObject
subclass, but I still would recommend it. It's simply a better design. If you change the superclass it will still work.
Source: NSObject Class Reference.

DrummerB
- 39,814
- 12
- 105
- 142
-
5+1 for "I still would recommend it" - the NSObject implementation could change, you could change your class's superclass, etc. It's not your class's job to infer things about its superclass's behavior. – Tim Sep 14 '12 at 16:26
-
4No, really, your code is **wrong** if you don't `self = [super init...];`. It doesn't matter what `NSObject`'s implementation does; use the pattern everywhere consistently to minimize fragility and because following the documented correct patterns leads to more maintainable apps. – bbum Sep 14 '12 at 17:35