> ## 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 Type-Casting Pattern

A type-casting pattern evaluates whether a value's dynamic runtime type matches a specified type. It is utilized exclusively within pattern-matching contexts, such as `switch` statements, `catch` clauses, and `if case` or `guard case` control flows.

The pattern relies on Swift's dynamic type system to resolve class hierarchies, protocol conformances, or existential containers at runtime. It manifests in two distinct syntactic forms: the `is` pattern and the `as` pattern.

## The `is` Pattern

The `is` pattern performs a pure type check. It evaluates to `true` if the runtime type of the matched value is equivalent to, a subclass of, or conforms to the specified right-hand type. It does not extract or bind the value.

**Syntax:**

```swift theme={"dark"}
is <Type>
```

**Structural Example:**

```swift theme={"dark"}
protocol CustomProtocol {}
let unknownValue: Any = 42

switch unknownValue {
case is Int:
    // Execution enters here if dynamic type is Int.
    // The value itself is not bound or casted for use.
case is CustomProtocol:
    // Execution enters here if dynamic type conforms to CustomProtocol.
default:
    break
}
```

## The `as` Pattern

The `as` pattern combines type checking with value binding. If the dynamic type of the subject matches the specified type, the pattern succeeds, and the subject is conditionally cast to that type. The resulting casted value is then bound to a constant or variable defined by a sub-pattern (typically a value-binding pattern like `let` or `var`).

**Syntax:**

```swift theme={"dark"}
<pattern> as <Type>
```

**Structural Example:**

```swift theme={"dark"}
let unknownValue: Any = "Swift"

switch unknownValue {
case let integerConstant as Int:
    // Matches if dynamic type is Int.
    // Binds the casted Int to 'integerConstant'.
case var stringVariable as String:
    // Matches if dynamic type is String.
    // Binds the casted String to a mutable 'stringVariable'.
default:
    break
}
```

## Technical Characteristics

* **Implicit Conditional Casting:** Within a pattern-matching context, the `as` keyword behaves analogously to the conditional cast operator (`as?`). If the runtime cast fails, the pattern simply fails to match, and control flow proceeds to the next branch. It does not trigger a runtime trap (unlike the forced cast operator `as!`).
* **Non-Exhaustiveness:** Because type-casting patterns evaluate dynamic types at runtime (often against `Any` or `AnyObject`), the compiler cannot guarantee that all possible types have been evaluated. Consequently, `switch` statements utilizing type-casting patterns are inherently non-exhaustive and require a `default` case or a catch-all pattern.
* **Protocol Existentials:** When matching against a protocol type, the type-casting pattern unwraps the existential container. If the underlying concrete type conforms to the protocol, the pattern succeeds, and the `as` pattern binds the value typed as the protocol existential.

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