已编译确实识别子类化的 NSWindow 实例

Compiled does recognize subclassed NSWindow Instance

我像这样子类化了 NSWindow。

class MainWindow : NSWindow
{ var bo : Bo?

  override func keyDown(with event: NSEvent) {
    //super.keyDown(with: event)
    print("Keydown \(event.keyCode) - modifier \(event.modifierFlags)  char: \(event.characters)")

    if (bo != nil && event.characters == "+") { 
      // do something with bo
    }
  }

  public func setBo(_ bo : Bo)
  { self.bo = bo
  }

}

在 AppDelegate 中,我像这样实例化了一个 window

window = MainWindow(   
        contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
        styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
        backing: .buffered, defer: false)

很好用,键盘事件打印出来了。

但是调用时

window.setBo(bo)

编译器声明 Value of type 'NSWindow' has no member 'setBo'

将 AppDelegate 中的声明更新为

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: MainWindow!  // << here !!