Swift (1.0) inheritance is driving me nuts beyond believe. I can believe have have to ask something that as simple as this but I can't figure it out. I have my custom view which I want to be able create programmatically with init(frame: CGRect) as well as load it from story board in which case init(coder aDecoder: NSCoder) should be used for initialization. in both cases I want to call my custom setup method. Like so :
import UIKit
class CustomView: UIView {
override convenience init(frame: CGRect) {
super.init(frame: frame)
self.myCustomSetup()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.myCustomSetup()
}
func myCustomSetup() {
self.backgroundColor = UIColor.redColor()
}
}
this does not seem to work, from swift documentation I understand there should only be one designated initializer for class which all the convenience initializers call. In UIView case this should be init(coder aDecoder: NSCoder). I however can not create aDecoder: NSCoder
in init(frame: CGRect)
neither can I pass nil as its not optional. What should I do ? How can I override them both ?
initWithFrame:
is also a designated initializer. – Leo NataninitWithFrame:
andinitWithCoder:
are as different as can be. – Leo Natan