> 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/enum-case-pattern.md).

# enum case pattern

[Swift](/ios/swift.md) ⟩ [Patterns](/ios/swift/pattern-matching.md) ⟩

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="info" %}
Swift uses various <mark style="color:orange;">**overloads**</mark> of the <mark style="color:purple;">**`~=`**</mark> operator to do <mark style="color:red;">**pattern matching**</mark>, which also lets us <mark style="color:orange;">**define our own overloads**</mark> ... 👉 [Sundell](https://www.swiftbysundell.com/articles/pattern-matching-in-swift/#under-the-hood-with-custom-matching)
{% endhint %}
{% endtab %}

{% tab title="💈範例" %}
💾 程式：[paiza.io](https://paiza.io/projects/6M1XMsa6-ev9TWJhRcIjuQ?language=swift)

⬆️ 需要：[CaseReflectable](/ios/swift/debugging/casereflectable.md)

```swift
// ------------
//     Enum
// ------------

// enum with associated values
// (conforms to `CaseReflectable`)
enum Enum: CaseReflectable {
    case int(Int)
    case int2(Int)
    case person(name: String, age: Int)
    case str(String)
}

// ------------
//     main
// ------------

let a: Enum = .int(3)

Enum.int ~= a        // true
Enum.int2 ~= a       // false

let joe = Enum.person(name: "joe", age: 8)

Enum.person ~= joe   // true
Enum.int ~= joe      // false
```

{% endtab %}

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

* [x] Erica ⟩ [Challenge: Filtering associated value enumeration arrays](https://ericasadun.com/2017/01/31/challenge-filtering-associated-value-enumeration-arrays/)
* [x] Austin Zheng ⟩ [Custom pattern matching in Swift](http://austinzheng.com/2014/12/17/custom-pattern-matching/) - custom array matching
* [x] Ole ⟩ [Pattern Matching in Swift](https://oleb.net/blog/2015/09/swift-pattern-matching/) - generic \~= operator
* [ ] Sundell ⟩&#x20;
  * [ ] [Five powerful, yet lesser-known ways to use Swift enums](https://www.swiftbysundell.com/articles/powerful-ways-to-use-swift-enums/)
    * [ ] [Cases as Functions](https://www.swiftbysundell.com/articles/powerful-ways-to-use-swift-enums/#cases-as-functions) ⭐️
  * [ ] [Pattern matching in Swift](https://www.swiftbysundell.com/articles/pattern-matching-in-swift/) ⟩&#x20;
    * [ ] [Under the hood with custom matching](https://www.swiftbysundell.com/articles/pattern-matching-in-swift/#under-the-hood-with-custom-matching) - pattern match against any kind of `Equatable` Errors.
      {% endtab %}

{% tab title="📘 手冊" %}

* Swift Ref ⟩ [Patterns](https://docs.swift.org/swift-book/ReferenceManual/Patterns.html) ⟩ [enum-case-pattern](https://docs.swift.org/swift-book/ReferenceManual/Patterns.html#grammar_enum-case-pattern)
* Swift Lib ⟩ [Equatable](https://developer.apple.com/documentation/swift/equatable) ⟩ [pattern-matching operator](https://developer.apple.com/documentation/swift/1539154) (`~=`)
  {% endtab %}

{% tab title="🗣 討論" %}

* [How to filter array of enums with associated values?](https://stackoverflow.com/questions/30973826/how-to-create-a-predicate-to-filter-array-of-enums-with-associated-values-in-swi)
* [\~= operator in Swift](https://stackoverflow.com/questions/38371870/operator-in-swift) ⭐️
* [Comparing enum cases while ignoring associated values](https://forums.swift.org/t/comparing-enum-cases-while-ignoring-associated-values/15922)
  {% endtab %}

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

* [enum](/ios/swift/type/category/basic/enum.md)
* [operator (\~=)](/ios/swift/pattern-matching/operator.md)
* [filter cases](/ios/swift/type/category/basic/enum/filter-cases.md)
  {% endtab %}
  {% endtabs %}
