Search code examples
swiftmacosappkit

Compiled does recognize subclassed NSWindow Instance


I subclassed NSWindow like this.

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
  }

}

In the AppDelegate I instantiate a window like this

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

That works fine, the Keyboard Events are printed out.

But when calling

window.setBo(bo)

the compiler states Value of type 'NSWindow' has no member 'setBo'


Solution

  • Update declaration in the AppDelegate as

    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        var window: MainWindow!  // << here !!