I wish to subclass NSWindowController and supply several of the standard init methods (e.g., init, initWithWindow, initWithWindowNibName). At the moment, I have each of the overriden methods call its equivalent super i.e., init calls [super init], initWithWindow calls [super initWithWindow] and so on and so forth.
Is this the proper way of doing things? Or am I setting this up way too generically and should I just provide a single init method that calls whatever super method necessary?
How does NSWindowController actually implement its init methods? According to the documentation, initWithWindow is the ‘default’ initializer – supposedly ‘default’ meaning that the other initialization methods call initWithWindow. Does that mean that when I subclass, I should only have a single initialization method that calls the class’ super?
I am getting confused to the point where I start to laugh hysterically and my dog looks funny at me 😉
Each class should have one designated initializer. When subclassing, you will typically elect the initializer that contains the most custom parameters, and term it your ‘designated’. The important part is that all other initializers (to include, in particular, the default initializer described for the parent classr), call up to this initializer. Only the ‘designated’ initializer calls super, all others call self.
For example:
So, let’s say you have several initializers with various options:
This custom init has the most parameters, and is your new ‘designated’. You’ll notice that it calls [super init]:
You have an additional custom init, but only takes one parameter. Notice that it calls self (NOT super):
Finally, override the classes default initializer (as specified in documentation):
To summarize: You may implement as many custom initializers as you’d like in your subclass. One initializer should be deemed your “designated” initializer, and only this method should implement [super init] (where init is the designated initializer of the super class, which may not be ‘init’). All the other initializers should call [self init] (where init is the designated initializer for your subclass).