'unrecognized selector' error happens when creating a subclass that inherits from NSWindow

32 views Asked by At

I want create a SettingWindow class which inherits from NSWindow, the code works fine in compile stage.

But when execute let settingWindow = new SettingWindow(), below error happens.

-[SettingWindow windowWithContentViewController:]: unrecognized selector sent to instance 0x1557574f0

This is sample code.

import Cocoa

final class SettingWindow: NSWindow {
    static let shared = SettingWindow()

    private let tabViewController: NSTabViewController

    private init() {
        tabViewController = NSTabViewController()
        tabViewController.tabStyle = .toolbar
        tabViewController.title = "Setting"

        super.init(contentViewController: tabViewController)
    }
}

I also try the code below, it just works. This make me confused.

import Cocoa

final class SettingWindow: NSWindow {
    static let shared = SettingWindow()

    private let tabViewController: NSTabViewController

    private init() {
        tabViewController = NSTabViewController()
        tabViewController.tabStyle = .toolbar
        tabViewController.title = "Setting"

        super.init(contentRect: .zero, styleMask: [.titled, .closable], backing: .buffered, defer: false)
        contentViewController = tabViewController
    }
}

I'm new to Swift and macOS develop, I want why this happen.

0

There are 0 answers