NSWindowController subclass in Swift

In trying to convert some Objective-C to Swift, I have a subclass of NSWindowController and want to write a convenience initializer. The documentation says

You can also implement an NSWindowController subclass to avoid requiring client code to get the corresponding nib’s filename and pass it to init(windowNibName:) or init(windowNibName:owner:) when instantiating the window controller. The best way to do this is to override windowNibName to return the nib’s filename and instantiate the window controller by passing nil to init(window:).

My attempt to do that looks like this:

class EdgeTab: NSWindowController
{
    override var windowNibName: NSNib.Name? { "EdgeTab" }
    
    required init?(coder: NSCoder)
    {
        super.init(coder: coder)
    }
    
    convenience init()
    {
        self.init( window: nil )
    }
}

But I'm getting an error message saying "Incorrect argument label in call (have 'window:', expected 'coder:')". Why the heck is the compiler trying to use init(coder:) instead of init(window:)?

You need to add

    override init(window: NSWindow?) {
        super.init(window: window)
    }

in your subclass.

See https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/

which say:

Rule 1

If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

Rule 2

If your subclass provides an implementation of all of its superclass designated initializers — either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition — then it automatically inherits all of the superclass convenience initializers.

The (unstated) corollary here is that if you only override some of the superclass's designated initializers, you inherit none of them.

in your case, you'd overridden init(coder:) not init(window:), so the compiler didn't 'see' an init(window:) at all.

I'm not smart enough to figure this out on my own, I asked an LLM and then asked it to show me the documentation, because you really can't trust those guys. ;)

NSWindowController subclass in Swift
 
 
Q