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.

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).
// 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.
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.
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.
// 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.
// 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.
// 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:
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:
if case let .success(data) = networkResult {
    // Evaluates to true if networkResult is .success, binding 'data' locally
}
In a for case loop:
// 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
}
Master Swift with Deep Grasping Methodology!Learn More