swiftPunk
swiftPunk

Reputation: 1

How can I remove or disable some initialization from NSView class in my sub?

I want remove (frame: ) and (coder: ) initialization from my sub class coming from my NSView super class, I tried this code but I was unable to do it, how can I make my sub just be initialized with init()? I have to say I tried to use override but it seems cannot help me to reach my goal of having just init() as initialization.

class MyNSView: NSView {
    
    convenience init() {
        self.init()
    }
    
}

Upvotes: 0

Views: 75

Answers (2)

vadian
vadian

Reputation: 285082

No, you can't remove it.

init(frame: is the designated initializer of NSView

As convenience initializer you have to call it – on self, not on super

class MyNSView: NSView {
    convenience init() {
        self.init(frame: .zero)
    }
}

However if the view is designed in Interface Builder then init(coder will be called.

Upvotes: 1

Tahmid108
Tahmid108

Reputation: 45

Try using required keyword while implementing the init() method.

class MyNSView: NSView {
    required init() {
        super.init(frame: .zero)
    }
}

You'll be able to initialize instances of your class by simply using

let myView = MyNSView()

Upvotes: 0

Related Questions