本文為您介紹Swift_可選鏈的說明,具體操作請看面的內容
Swift_可選鏈點擊查看源碼
//可選鏈
func test() {
class Person {
//可選屬性可能為nil或Residence類
var residence: Residence?
}
class Residence {
var numberOfRooms = 1
}
let john = Person()
john.residence = Residence()
//可選獲得
var roomCount = john.residence?.numberOfRooms
print(roomCount)
//強制獲得
roomCount = john.residence!.numberOfRooms
print(roomCount)
// if獲得
if let roomCount = john.residence?.numberOfRooms {
print(roomCount)
}
/* print
Optional(1)
Optional(1)
1
*/
}
謝謝您的訪問.
【Swift_可選鏈】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!