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

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"
}
}
主站蜘蛛池模板: 金寨县| 三河市| 凌海市| 虞城县| 株洲县| 江西省| 广昌县| 平陆县| 安泽县| 哈巴河县| 吉木乃县| 福建省| 永康市| 盐山县| 噶尔县| 酉阳| 河南省| 辉南县| 邢台市| 花莲市| 康马县| 石渠县| 花莲市| 霍山县| 临夏县| 克山县| 加查县| 邵阳市| 华亭县| 和平区| 孝义市| 垣曲县| 临猗县| 苏尼特右旗| 丹棱县| 鱼台县| 海丰县| 乐平市| 杨浦区| 伊吾县| 杂多县|