Consider the two classes:
class A {
var x: Int
init(x: Int) {
self.x = x
}
convenience init() {
self.init(x: 0)
}
}
class B: A {
init() {
super.init() // Error: Must call a designated initializer of the superclass 'A'
}
}
I don't see why this isn't allowed. Ultimately, each class's designated initializer is called with any values they need, so why do I need to repeat myself in B
's init
by specifying a default value for x
again, when the convenience init
in A
will do just fine?
This is Rule 1 of the "Initializer Chaining" rules as specified in the Swift Programming Guide, which reads:
Rule 1: Designated initializers must call a designated initializer from their immediate superclass.
Emphasis mine. Designated initializers cannot call convenience initializers.
There is a diagram that goes along with the rules to demonstrate what initializer "directions" are allowed: