我在函数中定义了一个struct,无论我调用该函数多少次,struct definition似乎总是第一次调用函数.
代码:
var g = 0
func f() {
struct InnerStruct{
static var attr:Int = g
}
println("static attr value is \(InnerStruct.attr), g is \(g)")
}
f()
g++
f()
g++
f()
Run Code Online (Sandbox Code Playgroud)
结果是:
static attr value is 0, g is 0
static attr value is 0, g is 1
static attr value is 0, g is 2
Program ended with exit code: 0
Run Code Online (Sandbox Code Playgroud)
我对swift不熟悉,可以解释一下为什么?