开发者

Objective C - How do I inherit from another class?

开发者_如何学Python- (id) init
{
     [super init];
     //initialitation of the class
     return self;
}

I know that when I am inheriting from another class I am suppose to call super.init

Does this apply to "inheriting from NSObject" ?


Yes, usually you have something like:

- (id) init
{
    if (self = [super init]) {
        // instantiation code
    }

    return self;
}


Technically, yes, because Apple's documentation says init... methods should always include a call to super. However at present, the NSObject implementation of -init does nothing, so omitting the call wouldn't prevent your code from working.

The downside of omitting the call to super is that your code wouldn't be as robust to future changes; for example if you later changed the inheritance, or if (God forbid) Apple changed NSObject's -init method so that it actually did something essential.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜