← Back to Swift Course | Chapter 3: Control Flow | Lesson 8 of 8

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.
Syntax
swift
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

markup
// 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))")
}

Matching Ranges

A case can match a value against a numeric range, which is useful for grouping values into categories.

Example: Matching Ranges

markup
// 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")
}

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

markup
// 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)")
}
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
  1. Thinking pattern matching is only for switch on enums; it also works with tuples, ranges, and optional binding.
  2. Forgetting that a case let pattern binds new local constants that only exist within that case's body.
  3. Overlooking that case order matters -- a broader pattern earlier in a switch will shadow a more specific one written after it.
Chapter Summary
  • Pattern matching in Swift appears in switch, if case, and for case constructs.
  • Tuple patterns can match multiple values and destructure them at once.
  • Range patterns (case 1...5) match a value falling within that range.
  • if case lets 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.