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

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
}
}
主站蜘蛛池模板: 怀来县| 凤凰县| 安西县| 盐山县| 镇江市| 泾阳县| 江永县| 襄城县| 巴林右旗| 错那县| 盐城市| 腾冲县| 洪湖市| 武川县| 黎平县| 奇台县| 龙井市| 荣昌县| 广丰县| 东方市| 时尚| 闽清县| 湖南省| 洛扎县| 温泉县| 庆阳市| 临泽县| 延川县| 嵩明县| 江门市| 靖西县| 固始县| 龙山县| 潞城市| 宁蒗| 额敏县| 蓬溪县| 精河县| 河北区| 秀山| 温泉县|