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

Conformance in an extension

The second way to declare a conformance is to add the conformance to an extension. The main benefit is that you can add functionalities, in the form of extensions, to existing types. The other main benefit of declaring a conformance inside of an extension is that you can scope this conformance to a particular file or module, with the private modifier.

For example, suppose that we want to add the toggle method to the Bool type, but only for the current file or your framework. You may not want it to leak outside, as the implementation may conflict with another one:

internal extension Bool: Toggling {
mutating func toggle() {
self = !self
}
}

var isReady = false
isReady.toggle()
assert(isReady)
主站蜘蛛池模板: 金坛市| 土默特左旗| 永安市| 长岭县| 枞阳县| 施甸县| 江西省| 普定县| 微博| 南昌县| 尤溪县| 南昌县| 深圳市| 安乡县| 镇安县| 兴义市| 北碚区| 犍为县| 石棉县| 宜章县| 同德县| 广元市| 宜都市| 扎赉特旗| 吉林省| 巴林右旗| 平舆县| 三原县| 盘锦市| 河北省| 石台县| 鄂尔多斯市| 宝丰县| 荆门市| 手游| 赤城县| 旺苍县| 中阳县| 钟山县| 天镇县| 潢川县|