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

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"
}
}
主站蜘蛛池模板: 区。| 得荣县| 泸水县| 得荣县| 永济市| 无棣县| 枣阳市| 广水市| 贺州市| 台南市| 涿鹿县| 锡林郭勒盟| 高台县| 盐津县| 台东县| 麻栗坡县| 台东县| 静乐县| 镇平县| 嘉兴市| 桦甸市| 博野县| 桐庐县| 汤阴县| 洪雅县| 本溪市| 洪洞县| 米林县| 筠连县| 洞头县| 若尔盖县| 古田县| 湟中县| 禄丰县| 祁门县| 屯昌县| 柏乡县| 丹江口市| 平安县| 虎林市| 河源市|