【发布时间】:2016-07-30 18:02:19
【问题描述】:
在本地范围之外声明的变量在范围内仍然可用/可访问。因此,我想如果我在范围内重新声明变量,编译器会告诉我一个重新声明错误。
在下面的代码sn-p中,常量tipPercentage在if作用域外声明,在if作用域内设置
let totallBill = 95.00
let tipPercentage: Double
let rating = 3
if rating == 5 {
tipPercentage = 0.25
} else if rating >= 3 {
tipPercentage = 0.15
} else {
let tipPercentage = 0.10 //# error caused by the let
}
let totalPaid = totallBill + totallBill * tipPercentage
问题
我在 if 范围内重新声明了常量。我以为它会告诉一个redeclare variable error,但它却给出了“constant "tipPercentage" used before being initialized.”这是为什么?
非常感谢
【问题讨论】: