I have a general question about writing init methods in Objective-C.
I see it everywhere (Apple's code, books, open source code, etc.) that an init method should check if self = [super init] is not nil before continuing with initialisation.
The default Apple template for an init method is:
- (id) init
{
self = [super init];
if (self != nil)
{
// your code here
}
return self;
}
Why?
I mean when is init ever going to return nil? If I called init on NSObject and got nil back, then something must be really screwed, right? And in that case, you might as well not even write a program...
Is it really that common that a class' init method may return nil? If so, in what case, and why?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…