欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

swift 隱式可選型實例詳解

 更新時間:2017年06月05日 11:28:23   作者:追到夢的魔術(shù)師  
這篇文章主要介紹了 swift 隱式可選型實例詳解的相關(guān)資料,需要的朋友可以參考下

1、隱式可選型的基本使用

var errorMessage: String? = nil
errorMessage = "Not Found"
"The message is " + errorMessage!

隱式可選型的定義

var errorMessage: String! = nil
errorMessage = "Not Found"
"The message is " + errorMessage

隱式可選型不需要解包,所以隱式可選型容易出錯

以上程序當(dāng)errorMessage為nil時程序會報錯

2、隱式可選型的實際應(yīng)用

// 主要應(yīng)用在類的成員變量的初始化上
class City{

  let cityName: String
  unowned var country: Country
  init( cityName: String , country: Country){
    self.cityName = cityName
    self.country = country
  }
}

class Country{

  let countryName: String
  var capitalCity: City!

  init( countryName: String , capitalCity: String ){

    self.countryName = countryName

    self.capitalCity = City(cityName: capitalCity, country: self)
  }

  func showInfo(){
    print("This is \(countryName).")
    print("The capital is \(capitalCity.cityName).")
  }
}

let china = Country(countryName: "China", capitalCity: "Beijing")
china.showInfo()

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論