> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swift/pattern-matching/sentence-patterns/switch-case-let-...-where.md).

# switch case let ... where

[Swift](/ios/swift.md) ⟩ [Pattern Matching](/ios/swift/pattern-matching.md) ⟩ [Sentence Patterns](/ios/swift/pattern-matching/sentence-patterns.md) ⟩

{% hint style="success" %}

* <mark style="color:red;">**case**</mark>: [pattern matching](/ios/swift/pattern-matching.md).
* <mark style="color:red;">**let**</mark>: variable binding.
* <mark style="color:red;">**where**</mark>: condition on bound variable.
  {% endhint %}

{% tabs %}
{% tab title="💈範例" %}
💾 程式： [replit](https://replit.com/@pegasusroe/Pattern-Matching-switch-case#Media.swift)

```swift
import Foundation            // for URL

// -------------
//     Media
// -------------

/// enum with associated values
enum Media {
    case book(title: String, author: String, year: Int)
    case movie(title: String, director: String, year: Int)
    case website(title: String, url: URL, year: Int)
}

extension Media {
    
    /// media title
    var title: String {
        switch self {
        // 2: ⭐ switch case let 
        //    (pattern matching + data binding)
        // --------------------------------------------------------
        //  - labels can be omitted
        //  - `case let .book(x, y, z) == case .book(let x, let y, let z)`
        //  - `case let .book(x, _, _) == case .book(let x, _, _)
        case let .book   (t, _, _): return t
        case let .movie  (t, _, _): return t
        case let .website(t, _, _): return t
        }
    }
    
    /// media title too
    var title2: String {
        switch self {
        // 3: ⭐ switch case let 
        //    (pattern matching + binding multiple cases)
        // -----------------------------------------------
        case 
            .book   (let t, _, _), 
            .movie  (let t, _, _), 
            .website(let t, _, _): return t
        }
    }
    
    func isPublished(after year: Int) -> Bool {
        switch self {
        // 5: ⭐ switch case let ... where
        // -------------------------------
        //   (pattern matching + data binding + condition on binding)
        //  - Note:
        //    •  `case .a(let x), .b(let x) where x > 0` is NOT equivalent to
        //       `case .a(let x) where x > 0, .b(let x) where x > 0`
        //    •  `case let .a(x), .b(x) where x > 0` WON'T compile.
        //       "⛔️ error: `x` must be bound in every pattern"
        case
            // for demo of `where` clause only, not recommended
            .book(_, _, let y) where y > year, 
            .movie(_, _, let y) where y > year, 
            .website(_, _, let y) where y > year: 
                return true
            // recommended way
            // case let .book(_,_,y), .movie(_,_,y) ... : return y > year
            default: return false
        }
    }
}
```

{% endtab %}

{% tab title="📗 參考" %}

* [x] [Part 1: switch, enums & where clauses](https://alisoftware.github.io/swift/pattern-matching/2016/03/27/pattern-matching-1/) ⭐️
  {% endtab %}
  {% endtabs %}
