官术网_书友最值得收藏!

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
}
}
主站蜘蛛池模板: 冕宁县| 五河县| 景宁| 西青区| 泗水县| 乡城县| 土默特右旗| 惠水县| 泽州县| 丽水市| 大冶市| 沂南县| 澄城县| 马公市| 扎赉特旗| 南陵县| 扎鲁特旗| 金门县| 仙居县| 韩城市| 日照市| 女性| 银川市| 浦北县| 金寨县| 抚宁县| 安顺市| 全南县| 瓦房店市| 政和县| 嘉峪关市| 当涂县| 永宁县| 南昌县| 安西县| 汶上县| 县级市| 贡嘎县| 江城| 灵璧县| 南阳市|