爲什麼守衛讓x = x在一個方法內的行爲與外部不同?爲什麼警衛讓x = x表現出不同的範圍行爲?
下面的示例代碼被從Playground複製出來。
var x:Int? = 3
func foo(x: Int?) {
guard let x = x else {
return
}
print(x) // print "3\n"
}
foo(x)
guard let x = x else {
throw NSError(domain: "app", code: 0, userInfo: nil)
}
print(x) // print "Optional(x)\n"
我仍然看到其投射不同的範圍的行爲。請參閱編輯的文章 – Boon
Swift民間證實了這一點 - 顯然這是一個編譯器錯誤。 – Boon