Swift: Class properties

Just throwing this out there.

class MyClass {
    // "class stored properties not yet supported in classes; did you mean 'static'?"
    // class var classProperty = "classProperty" // does not work
    class func classFunction() {println("classFunction")} // works
}

MyClass.classFunction() // works

class MyClass2 {
    static var staticProperty = "staticProperty"
    class func classFunction() {println("classFunction")} // works
    static func staticFunction() {println("staticFunction")}
}

MyClass2.staticProperty // works
MyClass2.classFunction() // works
MyClass2.staticFunction() // works

One Comment

  • You can, however, make overridable computed class properties:


    class MyClass {
    class var classProperty: String { return "classProperty" }
    static var staticProperty = "staticProperty"
    }

    class MySubclass: MyClass {
    override class var classProperty: String { return "subclassProperty" } // works
    static var staticProperty = "substaticProperty" // doesn't work
    }