斯威夫特 - 什么'类var'意味着什么

lor*_*cel 7 swift

这是在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)

  • 这似乎是一种非常奇怪的语言措辞选择。 (2认同)