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

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
}
}
主站蜘蛛池模板: 灵宝市| 舟山市| 雷州市| 天等县| 南丰县| 泽普县| 庆城县| 峨眉山市| 朝阳市| 曲阜市| 建瓯市| 偏关县| 镇沅| 香格里拉县| 青神县| 龙口市| 黎川县| 基隆市| 武威市| 杭州市| 克东县| 白城市| 舒城县| 乐安县| 和平区| 林甸县| 隆德县| 昂仁县| 合江县| 呼玛县| 嘉荫县| 伊宁市| 二连浩特市| 额济纳旗| 普定县| 南雄市| 襄城县| 兴安盟| 高邑县| 博白县| 焦作市|