Pattern Matching
Pattern matching is a powerful way to check if a value fits a certain shape, and pull out its pieces at the same time.
In this page:
Syntax
switch (a, b) {
case (0, let y): // tuple pattern
// code
case 1...10: // range pattern
// code
default: break
}
if case pattern = value { }
Matching Tuples
A switch can match against a tuple's shape, and case let bindings let you extract its individual components.
Example: Matching Tuples
// Declare a constant `coordinate`, set to `(0, 5)`
let coordinate = (0, 5)
// Switch on `coordinate` and branch by case
switch coordinate {
// Case: `(0, 0)`
case (0, 0):
// Print "Origin" to the console
print("Origin")
// Case: `(0, let y)`
case (0, let y):
// Print "On the y-axis at \(y)" to the console
print("On the y-axis at \(y)")
// Case: `(let x, 0)`
case (let x, 0):
// Print "On the x-axis at \(x)" to the console
print("On the x-axis at \(x)")
// Case: `let (x, y)`
case let (x, y):
// Print "At (\(x), \(y))" to the console
print("At (\(x), \(y))")
}
Login to try C/C++/Java/PHP code in the editor
Matching Ranges
A case can match a value against a numeric range, which is useful for grouping values into categories.
Example: Matching Ranges
// Declare a constant `score`, set to `85`
let score = 85
// Switch on `score` and branch by case
switch score {
// Case: `90...100`
case 90...100:
// Print "Grade: A" to the console
print("Grade: A")
// Case: `80..<90`
case 80..<90:
// Print "Grade: B" to the console
print("Grade: B")
default:
// Print "Grade: C or below" to the console
print("Grade: C or below")
}
Login to try C/C++/Java/PHP code in the editor
if case for Single-Value Matching
When you only need to test one pattern without a full switch, if case provides a lightweight alternative.
Example: if case for Single-Value Matching
// Declare a constant `value` (a `Int?`), set to `7`
let value: Int? = 7
// Check whether `case .some(let number) = value`
if case .some(let number) = value {
// Print "Got a number: \(number)" to the console
print("Got a number: \(number)")
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Thinking pattern matching is only for
switchon enums; it also works with tuples, ranges, and optional binding. - Forgetting that a
case letpattern binds new local constants that only exist within that case's body. - Overlooking that case order matters -- a broader pattern earlier in a
switchwill shadow a more specific one written after it.
Chapter Summary
- Pattern matching in Swift appears in
switch,if case, andfor caseconstructs. - Tuple patterns can match multiple values and destructure them at once.
- Range patterns (
case 1...5) match a value falling within that range. if caselets you pattern-match a single value without a full switch statement.
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: