Reputation: 55
I have this superclass :
class Car {
var made : String
var model : String
var year : Int
var litres : Double
var hp : Int
init (made : String , model : String , year : Int , litres : Double , hp : Int) {
self.made = made
self.model = model
self.year = year
self.litres = litres
self.hp = hp
}
func carDescription () -> String {
return "The made of the car is \(made), model is \(model). The year is \(year), with litres of \(litres) and a horsepower of \(hp)"
} }
And this subclass :
class SuperCar : Car {
var doors : String
override func carDescription() -> String {
super.carDescription()
return "The made of the car is \(made), model is \(model). The year is \(year), with litres of \(litres) and a horsepower of \(hp). The doors of this car opens like \(doors)"
}
init(made: String, model: String, year: Int, litres: Double, hp: Int , doors : String){
// the line below gets the "Editor Placeholder in source file
super.init(made: String, model: String, year: Int, litres: Double, hp: Int)
self.made = made
self.model = model
self.year = year
self.litres = litres
self.hp = hp
self.doors = doors
}}
I've seen some tutorials(maybe old tutorials) and they teach the init() in the subclass don't have any arguments in them. But the Xcode I'm using now needs me to type in all the superclass' arguments in.
And after typing them in, I get the "Editor placeholder in source file" warning and the code does not compile correctly.
Upvotes: 0
Views: 155
Reputation: 285150
There are two major mistakes in the code.
super
super
initializer rather than the typesFinally you can omit everything after the super
call.
init(made: String, model: String, year: Int, litres: Double, hp: Int , doors : String){
self.doors = doors
super.init(made: made, model: model, year: year, litres: litres, hp: hp)
}
Upvotes: 2