这东西太基本了,我敢肯定有人问过它,但我找不到。
我正在 swift 3 操场上进行试验,我想运行这段代码:
import UIKit
var optionalVariable: Int? = 32
var anotherVariable: Int
if optionalVariable != nil{
print("This executed")
anotherVariable = 32
}
print(anotherVariable)
它无法运行,我收到此错误:
Playground execution failed: error: chapter4.playground:5:7:
error: variable 'anotherVariable' used before being initialized
print(anotherVariable)
那么有什么问题呢?为什么我不能使用anotherVariable当我给它赋值时的值?
如果我注释掉最后一行print(anotherVariable),代码运行并且输出打印为This executed.