Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView's designated initializers in swift

Tags:

ios

swift

So I've subclassed UIView like so

class CustomView: UIView {
}

And I could do something like

let customView = CustomView()

But when I override what I believe are the two designated initialisers for UIView i.e. init(frame:) and init(coder:) like so

class CustomView: UIView {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.backgroundColor = UIColor.redColor()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.redColor()
    }
}

I can no longer create the view like so as it complains that init() no longer exists

let customView = CustomView()

My understanding is that If I override all the designated initialisers, I should inherit all the convenience initializers as well.

So my question is. Is init() not a convenience initializer on UIView? Or is there another designated initializer on UIView that I haven't overrided yet?

like image 299
Edward Huynh Avatar asked Feb 17 '15 01:02

Edward Huynh


1 Answers

init is not a convenience initializer on UIView. init is not an initializer on UIView at all! It is an initializer on its superclass NSObject - UIView merely inherits it. And it is a designated initializer on NSObject. So when you implemented a designated initializer, you cut off inheritance of designated initializers - including init.

like image 73
matt Avatar answered Nov 10 '22 00:11

matt