1、隱式可選型的基本使用
var errorMessage: String? = nilerrorMessage = "Not Found""The message is " + errorMessage!
隱式可選型的定義
var errorMessage: String! = nilerrorMessage = "Not Found""The message is " + errorMessage
隱式可選型不需要解包,所以隱式可選型容易出錯(cuò)
以上程序當(dāng)errorMessage為nil時(shí)程序會(huì)報(bào)錯(cuò)
2、隱式可選型的實(shí)際應(yīng)用
// 主要應(yīng)用在類(lèi)的成員變量的初始化上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()
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選