WiL BaJaMas
WiL BaJaMas

Reputation: 55

Swift super.init() Editor placeholder in source file

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

Answers (1)

vadian
vadian

Reputation: 285150

There are two major mistakes in the code.

  1. You have to initialize the stored properties of the subclass before calling super
  2. You have to pass the values in the super initializer rather than the types

Finally you can omit everything after the supercall.

    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

Related Questions