Reputation: 2154
I've got the following classes:
class A {
let age:Int
init(age: Int) {
self.age = age
}
}
extension A {
convenience init?(ageString: String) {
guard let age = Int(ageString) else { return nil}
self.init(age: age)
}
}
class B: A {
private var name = ""
init(name: String) {
self.name = name
super.init(age: 0)
}
}
When I try to run B(ageString: "1")
I will get an error because that init doesn't exist.
But if B
doesn't have any init methods and I make name
public, it won't complain. But that's not what I want.
Is this because init?(ageString: String) {
is a convenience method, I can't do it non convenience because is created in an extension.
Are not the convenience init method inherited if there are designated methods in B
?
Upvotes: -1
Views: 51
Reputation: 535889
Are not the convenience init method inherited if there are designated methods in B?
No. Implementing a designated initializer in a subclass blocks inheritance of initializers from the superclass — unless you explicitly override, in the subclass, all the designated initializers of the superclass.
Upvotes: 1
Reputation: 285220
First of all don't you get an error that super
is not called in init(name:)
?
The convenience initializer is only available if you implement (override) init(age
in B
override init(age: Int) {
super.init(age: age)
}
Upvotes: 2