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

A subscript in Swift is a specialized construct that enables instances of a type—including classes, structures, and enumerations—or the type itself, to be accessed and mutated using bracket notation (`[]`). Within the context of classes, it functions as a hybrid between a method and a computed property, allowing zero or more input parameters to be mapped to a return value through explicitly defined `get` and `set` blocks.

## Instance Subscript Syntax

Instance subscripts are defined within the class body using the `subscript` keyword. The signature accepts zero or more input parameters and requires a return type.

```swift theme={"dark"}
class CustomClass {
    // Read-only subscript with a default parameter value
    // Invoked via instance[] or instance[5]
    subscript(index: Int = 0) -> Int {
        return 42
    }

    // Read-write subscript with explicit argument labels
    // Invoked via instance[row: 1, column: 2]
    subscript(row row: Int, column column: Int) -> String {
        get {
            return "Value at \(row), \(column)"
        }
        set {
            // Perform mutation using the implicitly named 'newValue'
            // or explicitly define a name: set(customName) { ... }
        }
    }
}
```

## Technical Characteristics

* **Parameter Constraints:** Subscripts can accept zero or more parameters of any type, including variadic parameters, and they fully support default parameter values. However, they explicitly **cannot** utilize `inout` parameters.
* **Overloading:** A single class can define multiple subscripts. The compiler resolves the appropriate subscript via type inference based on the arity, argument labels, and types of the arguments passed within the brackets, as well as the expected return type.
* **Setter Semantics:** In a read-write subscript, the `set` block receives a parameter matching the subscript's return type. If no explicit parameter name is provided in the `set` declaration, Swift implicitly provisions the constant `newValue`.
* **Argument Labels:** Unlike functions, subscript parameters do not have argument labels by default; their external parameter name is implicitly `_`. They actively do not exist at the call site unless explicitly defined. To enforce label usage, an external parameter name must be declared in the subscript signature. The most common and idiomatic approach is to repeat the parameter name to expose it as a label (e.g., `subscript(row row: Int, column column: Int)`), though distinct external names can also be used (e.g., `subscript(at index: Int)`).

## Type Subscripts (Class Subscripts)

In addition to instance-level subscripts, Swift allows subscripts to be defined on the class type itself. For classes, this is achieved using either the `static` or `class` modifier.

```swift theme={"dark"}
class BaseClass {
    // Statically dispatched type subscript (cannot be overridden)
    static subscript(identifier: Int) -> String {
        return "Static: \(identifier)"
    }
    
    // Dynamically dispatched type subscript (can be overridden by subclasses)
    class subscript(key: String) -> Int {
        get { return 0 }
        set { /* Mutation logic */ }
    }
}

class SubClass: BaseClass {
    // Overriding the class subscript
    override class subscript(key: String) -> Int {
        get { return 1 }
        set { /* Subclass mutation logic */ }
    }
}
```

The critical distinction in class architecture is the use of the `class` keyword versus `static`. Declaring a `class subscript` exposes the subscript to dynamic dispatch, permitting subclasses to override the implementation using the `override` keyword. Declaring a `static subscript` enforces static dispatch, sealing the implementation from subclass modification.

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