Designated initializers


James Walker
 

I was looking at the Xcode operation Edit > Convert > To Modern Objective-C Syntax, and what it does with NS_DESIGNATED_INITIALIZER markup.  I know, I'm late to the party.

I often have a subclass of NSWindowController with one initializer, say

(instancetype) initWithParameter: (NSString*) name;

that is implemented using -[NSWindowController initWithWindowNibName:].  But the rules say that a designated initializer must call a superclass designated initializer, while a convenience initializer must call some other initializer of my own class.  Since initWithWindowNibName: is not a designated initializer of NSWindowController, it seems that my initializer can’t be either kind of initializer.  Do I need to override NSWindowController’s designated initializers, plus override initWithWindowNibName:, and then have my own initializer call my own initWithWindowNibName:?  Seems like a lot of trouble for little benefit.  Nobody should ever be calling any other initializer of my subclass.


Quincey Morris
 

On Oct 4, 2018, at 14:48 , James Walker <list2@...> wrote:

But the rules say that a designated initializer must call a superclass designated initializer, while a convenience initializer must call some other initializer of my own class.  Since initWithWindowNibName: is not a designated initializer of NSWindowController, it seems that my initializer can’t be either kind of initializer.

Yes, this is kinda broken for NSWindowController subclasses because “init(windowNibName:owner:)” kinda oughta be a designated initializer but isn’t.

What you should be able to do is define “convenience init(parameter:)” but no other inits. That will let all superclass inits (both designated and convenience) get inherited, and your “init(parameter:)” is then allowed to call “across” to another convenience init — “init(windowNibName:owner:)” in this case. So, this compiles:

    convenience init(parameter: String) {
        self.init(windowNibName: ) // <— ’self’ not ’super’ because it’s been inherited
    }