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

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
}
}
主站蜘蛛池模板: 德江县| 莱西市| 盐山县| 卢龙县| 漾濞| 延川县| 灵璧县| 上犹县| 富平县| 富阳市| 美姑县| 信宜市| 瑞昌市| 蒙山县| 彰化市| 商城县| 新兴县| 襄汾县| 仙游县| 长寿区| 封开县| 社旗县| 广东省| 许昌县| 颍上县| 射阳县| 神木县| 乌鲁木齐市| 休宁县| 观塘区| 玛纳斯县| 余庆县| 棋牌| 临武县| 永寿县| 保靖县| 南木林县| 上思县| 大荔县| 内黄县| 万荣县|