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

Protocol extensions

Protocol extensions allow us to define behavior on protocols rather than in each type's inpidual conformance or global function. By creating an extension on a protocol, all conforming types automatically gain this method implementation without any additional modification. We can specify constraints that conforming types must satisfy before the methods and properties of the extensions are available when we define a protocol extension. For instance, we can extend our ExampleProtocol to provide default functionality as follows:

extension ExampleProtocol { 
var simpleDescription: String {
get {
return "The description is: \(self)"
}
set {
self.simpleDescription = newValue
}
}

mutating func adjust() {
self.simpleDescription = "adjusted simple description"
}
}
主站蜘蛛池模板: 三台县| 合作市| 新龙县| 松桃| 全州县| 海阳市| 石楼县| 宽城| 平邑县| 静乐县| 饶阳县| 若尔盖县| 吕梁市| 许昌县| 深水埗区| 松阳县| 阿拉善右旗| 资阳市| 武威市| 巴彦淖尔市| 潼南县| 邵阳市| 广水市| 乳山市| 马龙县| 合江县| 鄂伦春自治旗| 万年县| 德江县| 永仁县| 正宁县| 左贡县| 石家庄市| 河源市| 桦甸市| 陇南市| 辛集市| 千阳县| 蛟河市| 广昌县| 江川县|