这是在swift中实现单例的解决方案之一.我很困惑为什么'var'前面加了一个'class'.据我所知,swift不支持类变量,为什么'class var'在这种情况下工作?
class Singleton {
class var sharedInstance : Singleton {
struct Static {
static let instance : Singleton = Singleton()
}
return Static.instance
}
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*son 18
这不是一类变量,它是一类计算机属性,它是目前支持.
// Playground - noun: a place where people can play
class A {
// Fine:
class var computed: String {
return "Woo"
}
// Not supported (yet):
class var realVariable: String = "Woo"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3555 次 |
| 最近记录: |