Reputation: 65785
Can I use other initializers within my initializer?
class Car {
var manufacturer:String?
var speed:Int?
init (manufacturer manf:String){
manufacturer = manf
}
init(manufacturer manf:String, speed spd:Int){
manufacturer = manf // How can I use the other initializer here?
speed = spd
}
}
var b = Car(manufacturer: "bmw")
var k = Car(manufacturer: "kia", speed: 30)
Upvotes: 0
Views: 74
Reputation: 65785
I figured it out. I just need the convenience
before my secondary init
then I can use self.init
to call the original init
class Car {
var manufacturer:String?
var speed:Int?
init (manufacturer manf:String){
manufacturer = manf
}
convenience init(manufacturer manf:String, speed spd:Int){
self.init(manufacturer: manf)
speed = spd
}
}
Upvotes: 1