#38: Static vs Class Properties in Swift

Both static and class define type-level members, but there is one important difference: class allows overriding in subclasses, while static does not.

class Animal {
    class var sound: String { "?" }
}

class Dog: Animal {
    override class var sound: String { "woof" }
}

For structs and enums, only static is available:

struct Math {
    static let piTwice = Double.pi * 2
}

As a rule of thumb, prefer static unless you explicitly want subclass customization.


I'm also on Twitter and GitHub.

Subscribe via RSS.