开发者

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

This question already has answers here: 开发者_JAVA百科 Closed 11 years ago.

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.


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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜