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.

The wildcard pattern (_) is a syntactic construct in Swift used to match and explicitly ignore any value during pattern matching, assignment, or iteration. It acts as an anonymous placeholder that satisfies arity and structural requirements without binding the underlying value to a named variable in the local scope.

Mechanics

When the Swift compiler encounters a wildcard pattern, it performs the following operations:
  • Expression Evaluation: The expression yielding the value is fully evaluated, ensuring any side effects of the expression still occur.
  • Value Discarding: The resulting value is immediately discarded.
  • Memory Management: No memory is allocated for a local variable binding. However, if the evaluated expression returns an owned reference type, Automatic Reference Counting (ARC) must still perform a release operation immediately after evaluation to properly destroy and deallocate the discarded value.
  • Warning Suppression: It explicitly signals to the compiler that the omission of a variable binding is intentional, suppressing “unused value” warnings.

Syntax and Structural Application

The wildcard pattern can be substituted anywhere a standard identifier pattern or value-binding pattern is expected. Standalone Assignment Absorbs the return value of a function or expression to satisfy the compiler when the result is unneeded.
_ = expressionReturningValue()
Tuple Decomposition Selectively ignores specific elements within a compound type while extracting others. The wildcard maintains the structural arity of the tuple during decomposition.
let (_, y, _) = (10.0, 20.0, 30.0)
Control Flow Iteration Satisfies the syntax requirements of a for-in loop when the current iteration index or sequence element is not referenced in the execution block.
for _ in 0..<5 {
    // Execution block
}
Enumeration Associated Values Matches an enumeration case within switch statements or if case constructs while discarding one or more of its associated payload values.
enum NetworkError: Error {
    case notFound
}

enum NetworkResponse {
    case success(String)
    case failure(Int, Error)
}

let response: NetworkResponse = .failure(404, NetworkError.notFound)

switch response {
case .success(_):
    break // Matches .success regardless of the associated payload
case .failure(_, let error):
    print(error) // Ignores the first associated value (Int), binds the second (Error)
}
Closure Parameters Satisfies a closure’s expected type signature and parameter count without naming parameters that will not be invoked within the closure body.
let closure: (Int, String) -> Void = { _, _ in
    // Execution block
}
Function Argument Labels Omits the external argument label in function and method declarations. While technically classified by the Swift grammar as an omitted argument label rather than a strict pattern, it utilizes the identical wildcard syntax and conceptual behavior to allow callers to pass arguments without naming them at the call site.
func process(_ input: String) {
    // Execution block
}
Master Swift with Deep Grasping Methodology!Learn More