0Pricing
Swift Academy · Lesson

Value Binding and where Clauses

Bind matched values and add extra conditions.

Binding in a Case

A switch case can bind the matched value to a constant with case let x, making the value usable inside that case.

let value = 42
switch value {
case let n:
    print("The number is \(n)")
}

Binding Part of a Tuple

You can bind just the parts of a tuple you need, using let on specific positions.

let point = (3, 7)
switch point {
case (let x, 0):
    print("On x-axis at \(x)")
case (0, let y):
    print("On y-axis at \(y)")
case let (x, y):
    print("At \(x), \(y)")
}

All lessons in this course

  1. Exhaustive Switch on Values
  2. Matching Ranges and Tuples
  3. Value Binding and where Clauses
  4. Compound Cases and Fallthrough
← Back to Swift Academy