- 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數(shù)據(jù)分析與挖掘?qū)崙?zhàn)
- 數(shù)據(jù)挖掘原理與實踐
- 分布式數(shù)據(jù)庫系統(tǒng):大數(shù)據(jù)時代新型數(shù)據(jù)庫技術(第3版)
- 揭秘云計算與大數(shù)據(jù)
- 大數(shù)據(jù):從概念到運營
- 深入淺出MySQL:數(shù)據(jù)庫開發(fā)、優(yōu)化與管理維護(第2版)
- 數(shù)據(jù)革命:大數(shù)據(jù)價值實現(xiàn)方法、技術與案例
- Microsoft Power BI數(shù)據(jù)可視化與數(shù)據(jù)分析
- Flutter Projects
- 深入理解InfluxDB:時序數(shù)據(jù)庫詳解與實踐
- 改變未來的九大算法
- Mastering ROS for Robotics Programming(Second Edition)
- 數(shù)據(jù)指標體系:構建方法與應用實踐
- 智能與數(shù)據(jù)重構世界
- MySQL數(shù)據(jù)庫應用與管理