swiftcocoanswindow

NSWindow initialiser with screen parameter causes Fatal Error upon initialisation


I have a simple subclass of NSWindow, and have some logic that returns an NSWindow. When passing in screen into the initialiser of NSWindow, the app causes a

Fatal error: Use of unimplemented initializer 'init(contentRect:styleMask:backing:defer:)' for class 'myClass'

Here's a chunk of code:

class myClass: NSWindow {
    init() {
        ...

        super.init(contentRect: NSRect(x: 0, y: 0, width: 100, height: 100),
                   styleMask: .borderless,
                   backing: .buffered,
                   defer: false,
                   screen: /* returned NSScreen */) // this crashes

        ...
   }
}

I'm not sure what's causing this, I've checked that NSScreens.screens returns an array of two non-nil screens (for my two monitors), and I can fetch all necessary data from them.

Why does passing it into the initialiser crash?

(I've made sure the logic is not the issue here; testing with just NSScreen.screens[0] in the screen: parameter causes this to crash too.)

Thanks in advance.


Solution

  • Turns out Xcode was being horrible with error messages as usual. Ran this in a brand new test project and got the actual error of

    Must call a designated initializer of the superclass 'NSWindow'
    

    The initialiser in NSWindow with screen is its convenience initialiser, and can't be called from a subclass. Read here for more: Why can't Swift initializers call convenience initializers on their superclass?

    Initialising an NSWindow itself without subclassing may be a temporary solution depending on your app structure.