2

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.

tim
  • 184
  • 1
  • 10
  • Post a [mre] please. – Willeke Jul 25 '22 at 14:18
  • 1
    @Willeke This code is very much reproducible, it's as simple as it gets. Pass `NSScreen.screens[0]` into the `screen: ` parameter and you get a reproducible example. Thank you. – tim Jul 25 '22 at 23:03
  • I tried your code and got "Must call a designated initializer of the superclass 'NSWindow'" instead of "Use of unimplemented initializer 'init(contentRect:styleMask:backing:defer:)' for class 'myClass'". The code doesn't compile. Creating a minimal reproducable example will help you find the cause of the issue. – Willeke Jul 26 '22 at 09:06
  • Yeah, ended up getting the same thing – please see my updated answer. Thanks nonetheless. – tim Jul 26 '22 at 09:19

1 Answers1

0

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.

tim
  • 184
  • 1
  • 10