标签:
swift的init方法是没有返回值的,而OC的init方法就有返回值,返回类本身
即使我们不写init方法,系统也会为函数默认写一个空的init方法
struct Fahrenheit { var temperature:Double init(){ temperature = 32.0 } } var f = Fahrenheit()
struct Celsius { var temperature : Double init(fromFahrenheit fahrenheit:Double){ temperature = (fahrenheit - 32.0)/1.8 } init(fromKelvin kelvin:Double){ temperature = kelvin - 273.15 } } let boilingPointOfWater = Celsius(fromFahrenheit: 212.0) let freezingPointOfWater = Celsius(fromKelvin: 273.15)
只存在与类中。当类可可选类型的时候,且为nil。deinit方法即会调用
class Player { var coinsInpurse:Int init(coins:Int){ coinsInpurse = coins } func winCoins(coins:Int){ coinsInpurse += 10 } deinit{ coinsInpurse = 0 } } var playerOne:Player? = Player(coins: 100) playerOne = nil
标签:
原文地址:http://my.oschina.net/u/2346786/blog/523312