1

I have the following code:

class EditorWindow: NSWindow {
    @Binding var keycode : Int

    override func keyDown(with event : NSEvent) {
        super.keyDown(with: event)
        Swift.print("Caught a key down: \(event.keyCode)!")
    }

    init(keycode : Int){
        self.keycode = keycode
        super.init(contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
                   styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                   backing: .buffered, defer: false)
    }
}

Placing the self.keycode = keycode before the super.init gives me the error "'self' used in property access 'keycode' before 'super.init' call", (as in this question, which suggests to swap the order). If I swap the order, I get the error: "Property 'self.keycode' not initialized at super.init call" (as in this question which suggests the original order as the solution) - it seems whichever order I use I get an error - how do I get around this?

Asperi
  • 173,274
  • 14
  • 284
  • 455
Joshua Lin
  • 435
  • 5
  • 20

1 Answers1

1

You need to pass a Binding<Int> to the constructor:

    init(keycode : Binding<Int>){
        self._keycode = keycode
        super.init( // .....
    }
Yonat
  • 3,861
  • 2
  • 26
  • 34