> ## 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 Enum Case Pattern

An enum case pattern matches a value against a specific case of an enumeration. It is utilized to evaluate the structural identity of an enum instance and, when applicable, to extract and bind its associated values to local constants or variables through pattern matching.

The pattern is evaluated in control flow constructs that support pattern matching, specifically `switch` statements, `if case`, `guard case`, and `for case` loops.

## Syntax Mechanics

The base syntax requires the `case` keyword followed by the enumeration member. If the type is known to the compiler, the type name can be omitted using implicit member expression syntax (a leading dot).

```swift theme={"dark"}
// Explicit type
case EnumerationName.caseName

// Inferred type
case .caseName
```

## Associated Value Binding

When an enumeration case contains associated values, the enum case pattern integrates with a **value-binding pattern** to extract those values. Swift provides two syntactic approaches for binding associated values:

**1. Inline Binding:**
The `let` or `var` keyword is placed directly inside the associated value tuple, preceding the specific value(s) you wish to bind.

```swift theme={"dark"}
case .response(let statusCode, var body):
```

**2. Distributed Binding:**
The `let` or `var` keyword is placed immediately after the `case` keyword. This distributes the binding modifier to all associated values within the tuple.

```swift theme={"dark"}
case let .response(statusCode, body):
```

## Wildcard Matching

If an enumeration case has multiple associated values but only a subset is required for the current scope, the enum case pattern incorporates the **wildcard pattern** (`_`) to explicitly ignore specific values.

```swift theme={"dark"}
// Binds 'statusCode', ignores the second associated value
case .response(let statusCode, _):
```

## Pattern Composition

Enum case patterns can be composed with other patterns and clauses to create highly specific matching criteria.

**Matching Specific Values:**
Instead of binding an associated value to a variable, the pattern can match against a concrete literal or constant.

```swift theme={"dark"}
// Matches only if the first associated value is exactly 404
case .response(404, let body):
```

**Appending a `where` Clause:**
The pattern can be constrained further by appending a `where` clause, which evaluates a boolean expression using the newly bound variables.

```swift theme={"dark"}
// Matches only if the bound 'statusCode' falls within the specified range
case let .response(statusCode, _) where (200...299).contains(statusCode):
```

## Contextual Examples

**In a `switch` statement:**

```swift theme={"dark"}
switch networkResult {
case .success(let data):
    // Matches the success case and binds 'data'
case .failure(let error) where error.isFatal:
    // Matches the failure case, binds 'error', and evaluates the where clause
case .failure:
    // Matches any remaining failure cases without binding the associated value
}
```

**In an `if case` statement:**

```swift theme={"dark"}
if case let .success(data) = networkResult {
    // Evaluates to true if networkResult is .success, binding 'data' locally
}
```

**In a `for case` loop:**

```swift theme={"dark"}
// Iterates only over elements in the array that match the .success case
for case let .success(data) in resultsArray {
    // 'data' is bound for each matching iteration
}
```

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