J W
J W

Reputation: 878

Designated Initializers for subclasses in obj-c, super noob

Sorry if this is a noob question, but if my designated initializer for my Base Class looks like

- (id)initWithName:(NSString *)name {
   if (self = [super init]) {
      _name = [name retain];

   }
   return self;
}

Now if I had a child class that inherits from this class and wanted to call that initializer, do I do:

- (id)initWithSomethingElse:(NSString *)somethingElse name:(NSString *)name {
   if (self = [super initWithName:name]) {
        _somethingElse = [somethingElse retain];

   }
   return self;

}

Upvotes: 0

Views: 64

Answers (2)

justin
justin

Reputation: 104698

correct, that is how one initializes in objc.

Upvotes: 0

Jim
Jim

Reputation: 73936

Yes, that's correct. You can call any of the superclass' initialisers, in fact.

Upvotes: 1

Related Questions