QCD_IS_GOOD
QCD_IS_GOOD

Reputation: 465

Order of setting variables and super.init in init, both give errors

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?

Upvotes: 1

Views: 286

Answers (1)

Yonat
Yonat

Reputation: 4608

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

    init(keycode : Binding<Int>){
        self._keycode = keycode
        super.init( // .....
    }

Upvotes: 1

Related Questions