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

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"
}
}
主站蜘蛛池模板: 淮南市| 樟树市| 隆安县| 舞钢市| 双辽市| 甘南县| 五家渠市| 叶城县| 昌都县| 西林县| 株洲县| 桐乡市| 抚松县| 定安县| 昌图县| 屯昌县| 阿合奇县| 惠来县| 佛坪县| 柏乡县| 绥中县| 七台河市| 长顺县| 株洲市| 九龙县| 黔西县| 津南区| 永吉县| 交口县| 天台县| 微山县| 资溪县| 连城县| 北票市| 千阳县| 青龙| 逊克县| 襄汾县| 乐陵市| 花莲市| 宁强县|