I know must call a designated initializer of the superclass, I think init(type: UIButtonType)
had called a designated initializer, so I used it in subclass convenience initializer, but failed
class TSContourButton: UIButton {
enum ContourButtonSizeType {
case large
case small
}
convenience init(type:ContourButtonSizeType) {
self.init(type: .custom)
}
then, I tried this. It compiles okay. but, It doesn't look professional
class TSClass: UIButton {
convenience init(frame: CGRect, myString: String) {
self.init(frame: frame)
self.init(type: .custom)
}
so, I doubt that I may think wrong. So, I did some test. It successfully called super convenience initializer
. Why I can't use self.init(type: .custom)
in convenience initializer at my subclass of UIButton
?
class person: UIButton {
var name: String = "test"
override init(frame: CGRect) {
super.init(frame: .zero)
self.name = "one"
}
convenience init(myName: String) {
self.init(frame: .zero)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class man: person {
convenience init(mySex: Int) { // it successfully call superclass convenience initializer
self.init(myName: "info")
}
If, for say, name is your mandatory field, you implement all your initial set up in a function. And you should handle if
name
is not available. I'll keepsmall
as the default option if no type was provided.