Swift super.init()源文件中的编辑器占位符

时间:2017-09-01 21:33:49

标签: swift

我有这个超类:

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)"
} }

这个子类:

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
}}

我已经看过一些教程(也许是旧教程)并且他们教导子类中的init()并没有任何参数。但是我现在使用的Xcode需要我输入所有的超类'参数。

输入后,我在源文件"中获得了#34;编辑器占位符。警告和代码无法正确编译。

1 个答案:

答案 0 :(得分:1)

代码中有两个主要错误。

  1. 您必须在调用super
  2. 之前初始化子类的存储属性
  3. 您必须在super初始值设定项中传递,而不是类型
  4. 最后,您可以在super电话后省略所有内容。

        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)
        }