> ## 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.

# Swift Class Method

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.

```swift theme={"dark"}
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.

```swift theme={"dark"}
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`.

```swift theme={"dark"}
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.

```swift theme={"dark"}
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.

```swift theme={"dark"}
protocol TypeActionable {
    static func performAction()
}

class ConformingClass: TypeActionable {
    // Satisfies the 'static' protocol requirement but allows subclass overriding
    class func performAction() {
        // Implementation
    }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Swift Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
