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

A read-only subscript in Swift is a custom indexing interface that allows retrieval of values from an instance of a class, structure, or enumeration, but explicitly prohibits modification. It is defined by implementing only a getter within the `subscript` declaration and omitting the setter.

## Syntax

Swift provides two ways to declare a read-only subscript: using an explicit `get` block, or using a shorthand syntax that omits the `get` keyword entirely.

**Explicit Getter Syntax:**

```swift theme={"dark"}
subscript(parameter: ParameterType) -> ReturnType {
    get {
        // Return an appropriate value of ReturnType
    }
}
```

**Implicit Getter (Shorthand) Syntax:**
Because read-only subscripts are common, Swift allows the omission of the `get` block. The body of the subscript acts directly as the getter.

```swift theme={"dark"}
subscript(parameter: ParameterType) -> ReturnType {
    // Return an appropriate value of ReturnType
}
```

## Technical Characteristics

* **Immutability Enforcement:** Attempting to assign a value to a read-only subscript results in a compile-time error (`Cannot assign through subscript: subscript is get-only`).
* **Parameter Constraints:** Subscripts can accept any number of input parameters of any type, including variadic parameters. However, they cannot use `inout` parameters or provide default parameter values.
* **Return Type:** Unlike functions, subscripts must explicitly declare a return type. The getter must return an instance matching this declared type.
* **Overloading:** You can define multiple read-only subscripts on the same type. The Swift compiler resolves which subscript to invoke based on the parameter signature passed at the call site.
* **Type Subscripts:** By prefixing the `subscript` keyword with `static` (or `class` for class types), a read-only subscript can be applied to the type itself rather than an instance of the type.

## Implementation Example

The following demonstrates a structure utilizing the shorthand read-only subscript syntax to expose internal storage without allowing external mutation.

```swift theme={"dark"}
struct AbstractContainer {
    private var internalStorage: [Int]
    
    init(elements: [Int]) {
        self.internalStorage = elements
    }
    
    // Read-only subscript
    subscript(index: Int) -> Int {
        return internalStorage[index]
    }
}

let container = AbstractContainer(elements: [10, 20, 30])
let value = container[1] // Valid: Retrieves 20
// container[1] = 50     // Invalid: Compile-time error
```

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