🅿️protocol
🚧 施工中
Swift ⟩ type ⟩ category ⟩ protocol
A set of requirements, such as methods and properties, that can be adopted (or followed) by a type, such as a class, structure, or enumeration.
When a type adopts a protocol, it provides its own implementation of the protocol requirements, and is said to conform to that protocol.
🇹🇼 中文稱為「規範」或「協定」。
- protocol is a declaration. 
Protocol Requirements:
- methods 
- initializers 
- associated types 
- properties 
- inherited protocols 
// 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)
}- 5 Stars ⟩ Every SwiftUI protocol explained 
- weak self podcast ⟩ 2: Swift API 設計之原來我不會用 protocol 
- Swift ⟩ Protocols 
Last updated
Was this helpful?