> ## 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 Capture List

A capture list is a Swift language construct used within a closure to explicitly define the memory management rules and evaluation timing for variables and constants captured from the surrounding lexical scope. It dictates whether a captured reference is strong, weak, or unowned, and forces variables to be evaluated and captured at the exact moment of closure creation rather than at execution.

## Syntax

The capture list is written as a comma-separated list of variables enclosed in square brackets `[]`. It is placed at the beginning of the closure body, immediately following any closure attributes (such as `@MainActor` or `@Sendable`), and preceding any parameters, return type declarations, and the `in` keyword.

```swift theme={"dark"}
{ @MainActor [modifier variable1, modifier variable2] (parameter) -> ReturnType in
    // Closure body
}
```

If the closure relies on implicit parameters and return types, the `in` keyword must still be included when a capture list is present:

```swift theme={"dark"}
{ [modifier variable] in
    // Closure body
}
```

## Mechanics of Capture

By default, Swift closures capture variables from their surrounding scope **by reference**. This means the closure holds a reference to the original variable, allowing it to observe modifications made outside the closure and vice versa.

Introducing a capture list fundamentally changes this behavior depending on the type of the captured entity.

### 1. Value Types

When a value type (e.g., `Int`, `String`, `Struct`) is included in a capture list, Swift evaluates the variable at the time the closure is *created* and captures a constant **copy** of that value.

```swift theme={"dark"}
var counter = 0

// Default capture (by reference)
let defaultClosure = {
    print(counter) 
}

// Capture list (by value at creation time)
let captureListClosure = { [counter] in
    print(counter) 
}

counter = 10

defaultClosure()       // Prints: 10
captureListClosure()   // Prints: 0
```

*Note: Variables captured this way are immutable within the closure. Attempting to mutate `counter` inside `captureListClosure` will result in a compiler error.*

### 2. Reference Types

When a reference type (e.g., `Class` instance) is included in a capture list, the closure still captures a reference to the object, but the capture list allows you to modify the Automatic Reference Counting (ARC) semantics.

The modifiers applied in the capture list dictate the strength of the reference:

* **Implicit (Strong):** If no modifier is provided (e.g., `[self]`), the closure maintains a strong reference to the instance, incrementing its retain count.
* **`weak`**: The closure captures a weak reference. The retain count is not incremented. Because the instance can be deallocated while the closure is still alive, the captured reference is automatically wrapped in an `Optional` type inside the closure.
* **`unowned`**: The closure captures an unowned reference. The retain count is not incremented. Unlike `weak`, `unowned` does not wrap the reference in an additional `Optional`; it preserves the exact existing type of the variable. If the captured variable is already an `Optional`, the `unowned` reference remains an `Optional`. It assumes the captured instance will outlive the closure. Accessing an `unowned` reference after the underlying instance has been deallocated triggers a runtime crash.

```swift theme={"dark"}
{ [weak self, unowned delegate = self.delegate] in
    // 'self' is wrapped in an Optional (e.g., MyClass?)
    // 'delegate' retains its original type (e.g., DelegateType?)
}
```

## Variable Binding and Aliasing

Capture lists support arbitrary expression evaluation and aliasing. You can declare new local variables within the capture list that are scoped exclusively to the closure. These expressions are evaluated exactly once, at closure creation.

```swift theme={"dark"}
{ [newVariable = someObject.property, unwrappedValue = someOptional ?? defaultValue] in
    // newVariable and unwrappedValue are available here
}
```

This mechanism is frequently used to capture a specific property of an object without capturing the entire object, or to safely provide default fallback values for optionals at the boundary of the closure scope.

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