> 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/if-guard-case-let.md).

# if/guard case let

{% hint style="info" %}

* <mark style="color:red;">**case**</mark> : pattern matching.
* <mark style="color:red;">**let**</mark> : binds <mark style="color:purple;">associated values</mark>
  {% endhint %}

{% tabs %}
{% tab title="💈範例" %}
{% hint style="warning" %}
⭐️ 注意： [for case let ... where](/ios/swift/pattern-matching/sentence-patterns/for-case-let-...-where.md) 加條件的方式跟 [if/guard case let](/ios/swift/pattern-matching/sentence-patterns/if-guard-case-let.md) 不一樣❗️
{% endhint %}

```swift
// ⭐ if case let
//          ╭───── ⭐ pattern ─────╮    ↱ ⭐ value
if case let Media.movie(title, _, _) = movie { ... }

// ⭐ if case let
if case let .loaded(data) = state, 
   data.isEmpty                        // ⭐ additional conditions
{ ... }

// ⭐ guard case let
guard 
    case let .loaded(data) = state, 
    data.isEmpty                        // ⭐ additional conditions
else { ... }
```

{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
it’s just a more **compact** syntax when **pattern-matching** <mark style="color:red;">**against one case**</mark> ...&#x20;

```swift
// if case let
if case let pattern = value { ... }

// is equivalent to 
switch value { 
    case let pattern: ...
    // other cases
}
```

👉 [alisoftware](https://alisoftware.github.io/swift/pattern-matching/2016/05/16/pattern-matching-4/)
{% endhint %}

```swift
// if case let
if case let .person(name, age) = joe { ... }
// is equivalent to
if case .person(let name, let age) = joe { ... }
```

{% endtab %}

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

* [ ] [How Do I Write If Case Let in Swift?](https://fuckingifcaseletsyntax.com/)
* [ ] Majid ⟩ [Pattern Matching with case let](https://swiftwithmajid.com/2019/02/06/pattern-matching-with-case-let/)
* [ ] Use Your Loaf ⟩ [Swift If Case Let](https://useyourloaf.com/blog/swift-if-case-let/)
* [ ] alisoftware ⟩ [Pattern Matching, Part 4: if case, guard case, for case](https://alisoftware.github.io/swift/pattern-matching/2016/05/16/pattern-matching-4/)
  {% endtab %}

{% tab title="👥 相關" %}

* [enum](/ios/swift/type/category/basic/enum.md)
* [Mirror](/ios/swift/debugging/mirror.md)
  {% endtab %}
  {% endtabs %}
