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 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.
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.
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.
Master Swift with Deep Grasping Methodology!Learn More