8.1 聲明一個可空類型:
var errorCodeString: string?
8.2 可空實例綁定
var errorCodeString: string?
if let theError = errorCodeString {
}
可空類型拆解:所有的變量賦值最好都用這種方式
var errorCodeString: string?
errorCodeString = "404"
if let theError = errorCodeString {
print(theError)
}
8.3 隱式展開可空類型
會有問題募狂,只有當(dāng)明確知道值不可能為nil的情況下才能用這種類型驾锰。
var errorCodeString: string!
errorCodeString = "404"
print(errorCodeString)
8.4 可空鏈?zhǔn)秸{(diào)用
if let theError = errorCodeString, let errorCodeInteger = Int(theError),errorCodeInteger == 404 {
}
8.5 原地修改可空實例
errorCodeString?.append("PLEASE TRY AGAIN")
8.6 nil合并運算符:
如果errorDescription是nil的話就執(zhí)行No error
let description = errorDescription ?? "No error"