Patterns
A pattern describes the shape of a value rather than a single value. Swift uses
patterns to bind names in let/var declarations and to
match values in switch, if case, for case,
and catch. This reference covers each pattern kind with a short
example.
Wildcard pattern
The wildcard _ matches and ignores any value. Use it to discard
parts you don't need.
for _ in 1...3 { tick() } // ignore the loop value
let (_, y) = point // keep only y
Identifier and value-binding patterns
An identifier pattern matches any value and binds it to a name.
A value-binding pattern uses let or var
to bind matched parts of a composite value to new constants or variables.
switch point {
case let (x, y) where x == y: print("on diagonal: \(x)")
case (let x, 0): print("on x-axis at \(x)")
default: break
}
Tuple pattern
A tuple pattern is a comma-separated list of patterns in parentheses, matching a tuple element-by-element. The element patterns may themselves be any pattern, allowing rich destructuring.
let (name, (lat, lon)) = ("HQ", (37.3, -122.0))
Enumeration-case pattern
An enumeration-case pattern matches a specific case and can
extract its associated values via a nested pattern. It powers most
switch statements over enums and works with if case.
enum Result { case success(Int), failure(Error) }
if case let .success(code) = result {
print("got \(code)")
}
Optional pattern
An optional pattern, written as an identifier followed by
?, matches a non-nil value wrapped in an
Optional. It's sugar for matching .some(x).
let values: [Int?] = [1, nil, 3]
for case let n? in values {
print(n) // → 1, then 3 (nil skipped)
}
Type-casting patterns
Two patterns test or bind by dynamic type. The is
pattern matches if the value is of the given type (without binding); the
as pattern matches and binds the value cast to the
named type.
switch anyValue {
case is String: print("a string")
case let n as Int: print("int \(n)")
default: break
}
Expression pattern
An expression pattern matches using the ~= operator,
comparing the case's expression against the matched value. By default
~= uses ==, but ranges (and your own overloads) make it
far more flexible.
switch score {
case 0: print("zero")
case 1..<10: print("single digit") // Range ~= Int
default: print("large")
}
Tip: overload static func ~= (pattern:, value:)
to enable custom matching for your own types inside a switch.
Summary
Patterns let Swift match and destructure values declaratively. Binding patterns
(identifier, value-binding, tuple) pull data out; enumeration-case and optional
patterns work over sum types; type-casting patterns branch on dynamic type; and
expression patterns match via ~= for values and ranges. The wildcard
ties it together by matching anything you choose to ignore.