//Swift 没有静态初始化块。
//存储型类型属性在首次访问时惰性初始化,且只初始化一次,
//即使多个线程同时访问也是如此。
class Config {
static let table: [String: Int] = {
print("the block runs on first access")
return ["a": 1, "b": 2]
}()
//实例在 init() 中完成设置
var name: String
init() {
name = "config"
}
}
print("before the first access")
let a = Config.table["a"]!
//a is 1
print("a is \(a)")
//该块不会再次运行
let b = Config.table["b"]!
//b is 2
print("b is \(b)")