可選鏈是 Swift 中處理 nil 的安全方式。當屬性或方法可能不存在時,可用問號逐層存取,而不會直接崩潰。
例如,一個 Person 可能沒有 Residence,此時可用 john.residence?.numberOfRooms 取得房間數。若 residence 為 nil,整個表達式會回傳 nil。
若確定值存在,也可用驚嘆號強制取值;較安全的做法則是用 if let 或 guard let 進行綁定。