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 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:
is <Type>
Structural Example:
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:
<pattern> as <Type>
Structural Example:
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.
Master Swift with Deep Grasping Methodology!Learn More