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 method in Swift is a type-level method associated with a class itself rather than an instance of that class. Declared using the class keyword, these methods are invoked directly on the class type and, crucially, utilize dynamic dispatch, allowing them to be overridden by subclasses.

Syntax and Invocation

A class method is defined by prefixing the func keyword with the class modifier. It is invoked using dot syntax on the type name.
class BaseClass {
    class func executeTypeAction() {
        print("BaseClass action executed.")
    }
}

// Invocation
BaseClass.executeTypeAction()

Overriding Class Methods

Because class methods are dynamically dispatched, a subclass can provide its own implementation of a superclass’s class method using the override keyword.
class Subclass: BaseClass {
    override class func executeTypeAction() {
        print("Subclass action executed.")
    }
}

Subclass.executeTypeAction() // Prints: "Subclass action executed."

class vs. static

Both class and static define type methods, but they differ in their dispatch mechanisms and inheritance rules:
  • class func: Dynamically dispatched. Can be overridden by subclasses.
  • static func: Statically dispatched. Cannot be overridden. In the context of a class, declaring a method as static func is functionally identical to declaring it as final class func.
class DispatchExample {
    class func overridableMethod() {}
    static func nonOverridableMethod() {}
}

class DerivedExample: DispatchExample {
    override class func overridableMethod() {}
    
    // ERROR: Cannot override static method
    // override static func nonOverridableMethod() {} 
}

The self Keyword in Class Methods

Within the body of a class method, the implicit self property refers to the class type itself, not an instance of the class. This allows you to call other class methods or access class-level properties directly without explicitly prefixing them with the class name.
class ContextExample {
    class var typeIdentifier: String {
        return "ContextExampleType"
    }

    class func printContext() {
        // 'self' refers to the ContextExample type
        print("Executing within \(self.typeIdentifier)")
    }
}

Protocol Conformance

When a class conforms to a protocol that requires a type method (defined in the protocol using the static keyword), the class can satisfy this requirement using either a static func or a class func. Using class func satisfies the requirement while preserving the ability for subclasses to override the implementation.
protocol TypeActionable {
    static func performAction()
}

class ConformingClass: TypeActionable {
    // Satisfies the 'static' protocol requirement but allows subclass overriding
    class func performAction() {
        // Implementation
    }
}
Master Swift with Deep Grasping Methodology!Learn More