aryaxt
aryaxt

Reputation: 77646

Objective C - How do I inherit from another class?

- (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" ?

Upvotes: 2

Views: 449

Answers (2)

jlehr
jlehr

Reputation: 15617

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.

Upvotes: 0

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38636

Yes, usually you have something like:

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

    return self;
}

Upvotes: 4

Related Questions