Search code examples
objective-ciosdesignated-initializer

Designated initializer and passing arguments


I have this hierarchy:

CreateAnObjectClass : NSObject

MySecondClass : MyBaseClass
    MyBaseClass : NSObject

in CreateAnObjectClass I want to create an instance of MySecondClass method and i want to pass a @property (strong,nonatomic) NSDictionary* myTemplate to myBaseClass.

For example:

CreateAnObjectClass *testObj = [[MySecondClass alloc] initWithTemplate:myTemplate];

And I know that calls both initializers from MyBaseClass and MySecondClass.

(id)initWithTemplate:(NSDictionary*)myTemplate
{
  self = [super init]

  return self;
}

My question is how I should designe initializers to myTamplate can be a property at MyBaseClass?


Solution

  • Like this:

    - (id)initWithTemplate:(NSDictionary*)aTemplate
    {
      self = [super init]
      if (self){
        self.myTemplate=aTemplate;
      }
      return self;
    }
    

    Edit 1:

    Remember the following, calling this:

      self = [super init];
    

    On the MySecondClass will call the init method on the MyBaseClass