小编617*_*174的帖子

在函数中定义的Struct

我在函数中定义了一个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不熟悉,可以解释一下为什么?

struct swift

3
推荐指数
1
解决办法
149
查看次数

标签 统计

struct ×1

swift ×1