Reputation: 2207
I'm subclassing a class. I'm overriding a init method. This one: -(id)initWithSomething:(Something*)somet;
this would look like this (in the subclass)
-(id)initWithSomething:(Something *)somet with:(int)i{
if (self = [super init]) {
//do something
}
return self;
}
But now I want to call the init in the superclass too.
How would I now do this? Mayby this way?
-(id)initWithSomething:(Something *)somet with:(int)i{
if (self = [super init]) {
}
[super initWithSomething:somet];
return self;
}
Upvotes: 1
Views: 626
Reputation: 299355
-(id)initWithSomething:(Something *)somet {
if ((self = [super initWithSomething:somet])) {
// ...
}
return self;
}
One-and-only-one method should be your "designated initializer" for a class. All other initializers should call that one, and the designated initializer should call super
's designated initializer. (This is a general rule; there are a few exceptions such as in initWithCoder:
, but it is the normal approach.)
Upvotes: 2
Reputation: 10065
Typically like this:
-(id)initWithTarget:(CCNode *)someTarget
{
self = [super initWithTarget:someTarget];
if (self)
{
}
return self;
}
It's the responsibility of super to call the vanilla init selector if it needs to.
Upvotes: 2