---
description: 🚧 施工中
---

# protocol

[Swift](../..) ⟩ [type](..) ⟩ [category]() ⟩ protocol

{% hint style="success" %}
<mark style="color:yellow;">A set of requirements</mark>, such as <mark style="color:orange;">methods</mark> and [properties](../prop), that can be adopted (or followed) by a [type](..), such as a [class](basic/class), <mark style="color:orange;">structure</mark>, or [enumeration](basic/enum).

When a [type](..) adopts a <mark style="color:purple;">protocol</mark>, it provides its own implementation of the protocol requirements, and is said to <mark style="color:yellow;">conform to</mark> that protocol.

:flag\_tw: 中文稱為「<mark style="color:purple;">規範</mark>」或「<mark style="color:purple;">協定</mark>」。
{% endhint %}

{% tabs %}
{% tab title="🔴 主題" %}
* [inherit](protocol/inherit "mention")
* [require](protocol/require "mention")
* [protocol-hierarchy](protocol/inherit/protocol-hierarchy "mention")
* [protocol-extensions](protocol/protocol-extensions "mention")
* [generic](protocol/generic "mention")
* [conditional-conformance](protocol/conditional-conformance "mention")
* [combining-protocols](protocol/combining-protocols "mention")
* [typecasting-with-protocols](protocol/typecasting-with-protocols "mention")
* :scales: [some-any-generics](some-any-generics "mention")
{% endtab %}

{% tab title="👥 相關" %}
* <mark style="color:purple;">**protocol**</mark> is a [declaration](../../statement/simple/declaration "mention").
{% endtab %}

{% tab title="⭐️ 重點" %}
{% hint style="success" %}
Protocol Requirements:

* **methods**&#x20;
* **initializers**&#x20;
* **associated types**&#x20;
* **properties** &#x20;
* **inherited protocols**
{% endhint %}

{% hint style="info" %}
<mark style="color:purple;">**`Any`**</mark> is an <mark style="color:red;">**empty protocol**</mark> and <mark style="color:orange;">**everything**</mark> <mark style="color:red;">**implicitly**</mark> conforms to this protocol. 👉 [AppVenture.me](https://appventure.me/guides/swift_reflection/mirrors/creating_a_mirror.html)
{% endhint %}
{% endtab %}

{% tab title="💈範例" %}
```swift
// Some code
protocol SomeProtocol {

    // -----------------------------
    //     Property Requirements
    // -----------------------------
    // • can be stored/computed property
    
    // ⭐ gettable
    // can’t be fulfilled by a constant stored property 
    // or a read-only computed property.
    var mustBeSettable: Int { get set }
    
    // ⭐ settable
    // satisfied by any kind of property
    var doesNotNeedToBeSettable: Int { get }
    
    // ⭐ type property
    static var typeProperty: Int { get set }
    
    // -----------------------------
    //     Method Requirements
    // -----------------------------
    
    func method() -> Double         // ⭐ instance method
    static func typeMethod()        // ⭐ type method
    mutating func toggle()          // ⭐ mutating method
    
    // -----------------------------
    //     Init Requirements
    // -----------------------------
    
    init(someParameter: Int)
}
```
{% endtab %}

{% tab title="📗 參考" %}
* [x] Paul ⟩ [Swift Protocols: Tips and Tricks](https://www.hackingwithswift.com/articles/138/swift-protocols-tips-and-tricks)
* [ ] Sundell ⟩&#x20;
  * [ ] [The different categories of Swift protocols](https://www.swiftbysundell.com/articles/different-categories-of-swift-protocols/)
  * [ ] [Why can’t certain protocols, like Equatable and Hashable, be referenced directly?](https://www.swiftbysundell.com/questions/referencing-generic-protocols/)
* [ ] 5 Stars ⟩ [Every SwiftUI protocol explained](https://www.fivestars.blog/articles/swiftui-protocols/)
* [ ] Khawer ⟩ [When and how to use the Equatable and Identifiable protocols in Swift](https://khawerkhaliq.com/blog/swift-equatable-identifiable/)
* [ ] weak self podcast ⟩ [2: Swift API 設計之原來我不會用 protocol](https://weakself.dev/episodes/2) &#x20;
{% endtab %}

{% tab title="📘 手冊" %}
* Swift ⟩ [Protocols](https://docs.swift.org/swift-book/LanguageGuide/Protocols.html)
{% endtab %}
{% endtabs %}
