2014-06-30 26 views
0

說我的UIViewController有兩個屬性,var animal: Stringvar color: UIColor如何爲我在Swift中以編程方式創建的UIViewController子類創建init?

如何創建自定義init,以便我可以在代碼中輕鬆創建此代碼?

我想有:

init(animal: String, color: UIColor) { 
    self.animal = animal 
    self.color = color 
} 

,但我得到的是我不使用指定的初始化一個編譯器錯誤。我在做什麼錯了/我該如何做這件事?

回答

4

設置這些屬性後請致電super.init()。這應該夠了吧。你的方法應該是這樣的:

init(animal: String, color: UIColor) { 
    self.animal = animal 
    self.color = color 
    super.init() 
} 
+3

通常在Swift中,應該最後調用super.init。 –

+0

你說得對。更新了答案。 – bvogelzang

+0

super.init()不是UIViewController的指定初始值設定項 – aBikis