Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would [super init] ever return nil, when "super" is NSObject? [duplicate]

Possible Duplicate:
In Objective-C why should I check if self = [super init] is not nil?

In Objective-C book i am reading, it is said that when [init] message is sent to NSObject, on occasion it may return nil and we should check the return value, before sending more messages to what may end up being a nil.

self = [super init];

if (self) {
 do stuff
}

I ask you though, what needs to happen for an NSObject to not be able to init itself?

Edit: Question specifically deals with an instance where YourClass:NSObject.

like image 790
James Raitsev Avatar asked Jul 18 '11 01:07

James Raitsev


1 Answers

NSObject itself will never return nil on init, however other classes that inherit from it might, so it's considered good practice to always check the return value of init. For example, this will return nil:

 [[NSData alloc] initWithContentsOfFile:@"path/to/a/file/that/does/not/exist"];
like image 135
EdoDodo Avatar answered Oct 23 '22 11:10

EdoDodo