- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 119字
- 2021-07-02 14:45:00
Protocol extensions
With protocol extensions, it is possible to provide an implementation for the required methods, without letting the conforming types provide that implementation.
We have updated the Toggling protocol with an additional required member: isActive. With the protocol extension, we can declare a default implementation for our types, Bool and State. We can also provide a default implementation for any other type that would choose to conform to the Toggling protocol:
protocol Toggling {
mutating func toggle()
var isActive: Bool { get }
}
extension Toggling where Self == Bool {
var isActive: Bool {
return self
}
}
extension Toggling where Self == State {
var isActive: Bool {
return self == .on
}
}
推薦閱讀
- 輕松學大數據挖掘:算法、場景與數據產品
- Python廣告數據挖掘與分析實戰
- Python數據分析、挖掘與可視化從入門到精通
- 數據要素五論:信息、權屬、價值、安全、交易
- Enterprise Integration with WSO2 ESB
- 深入淺出MySQL:數據庫開發、優化與管理維護(第2版)
- Sybase數據庫在UNIX、Windows上的實施和管理
- 數據庫技術及應用教程
- 科研統計思維與方法:SPSS實戰
- 重復數據刪除技術:面向大數據管理的縮減技術
- INSTANT Android Fragmentation Management How-to
- 智慧的云計算
- Oracle 11g+ASP.NET數據庫系統開發案例教程
- Hands-On System Programming with C++
- 信息融合中估計算法的性能評估