- 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
}
}
推薦閱讀
- 數據浪潮
- 云數據中心基礎
- MySQL基礎教程
- Lean Mobile App Development
- Spark核心技術與高級應用
- 數據庫技術及應用教程
- MATLAB Graphics and Data Visualization Cookbook
- Flutter Projects
- 一本書講透Elasticsearch:原理、進階與工程實踐
- Unreal Engine Virtual Reality Quick Start Guide
- 從實踐中學習sqlmap數據庫注入測試
- Spring Boot 2.0 Cookbook(Second Edition)
- 大數據分析:R基礎及應用
- 大數據隱私保護技術與治理機制研究
- Tableau商業分析從新手到高手(視頻版)