# 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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swift/pattern-matching/sentence-patterns/if-guard-case-let.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
