2017-09-01 55 views
-1

我有這個超:斯威夫特super.init()的源文件編輯器佔位

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需要我輸入所有超類的參數。

輸入後,我得到「源代碼文件中的編輯器佔位符」警告,代碼無法正確編譯。

回答

1

代碼有兩個主要的錯誤。

  1. 你有調用super
  2. 前初始化子類的存儲性能你必須通過值在super初始化而不是類型

最後你在撥打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) 
    } 
+1

要添加,您不需要手動設置其他屬性。 super.init設置它們。 – PeejWeej

+0

的確,謝謝。我被明顯的錯誤所迷惑。 – vadian