Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A class property is a type-level property associated with a class definition itself rather than with any specific instance of that class. In Swift, class properties are declared using the class keyword, which enables dynamic dispatch and allows subclasses to override the property’s implementation.

Technical Characteristics

  • Computed Only: Swift restricts the class keyword to computed properties. You cannot declare a stored class property.
  • Overridability: The primary mechanical distinction of a class property is that it permits subclass overriding.
  • Type-Level Access: Class properties are accessed and mutated through the type name directly, or dynamically at runtime using the type of an instance.

Syntax and Dynamic Dispatch

To declare an overridable type property, use the class modifier followed by var and a computed getter (and optionally a setter). Because class properties utilize dynamic dispatch, their overridden implementations can be resolved at runtime polymorphically.
class BaseClass {
    // A computed class property (Overridable)
    class var overridableProperty: String {
        return "Base Implementation"
    }
    
    // A method demonstrating polymorphic access
    func printProperty() {
        print(type(of: self).overridableProperty)
    }
}

class SubClass: BaseClass {
    // Overriding the computed class property
    override class var overridableProperty: String {
        return "Subclass Implementation"
    }
}

// Upcasting a SubClass instance to BaseClass
let instance: BaseClass = SubClass()

// Demonstrating dynamic dispatch via type(of:)
let dynamicValue = type(of: instance).overridableProperty 
// Evaluates to "Subclass Implementation"

// Demonstrating dynamic dispatch via internal type(of: self)
instance.printProperty() 
// Prints "Subclass Implementation"

class vs. static

While both class and static define type properties, they dictate different dispatch mechanisms and storage capabilities:
  1. class Properties: Utilize dynamic dispatch. They are strictly computed properties that subclasses can override.
  2. static Properties: Utilize static dispatch. They can be either stored or computed properties. For computed properties, a static declaration is mechanically equivalent to a final class declaration, meaning it cannot be overridden by subclasses. However, this equivalence does not apply to stored properties, as the class keyword strictly prohibits stored properties, even when marked final.

Memory and Initialization

Because class properties are strictly computed, they do not occupy persistent memory for state storage. They execute their defined getter and setter blocks upon access. If state storage is required at the type level within a class hierarchy, a static stored property must be used. Stored static properties are lazily initialized on their first access and are guaranteed by the Swift runtime to be thread-safe during initialization.
Master Swift with Deep Grasping Methodology!Learn More