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

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)
主站蜘蛛池模板: 东丰县| 筠连县| 特克斯县| 乳山市| 黄石市| 仁布县| 个旧市| 新田县| 潜江市| 京山县| 灵山县| 湘乡市| 克拉玛依市| 新和县| 恩施市| 山阴县| 阿勒泰市| 平舆县| 彩票| 麻栗坡县| 长寿区| 古蔺县| 武汉市| 波密县| 永嘉县| 富裕县| 磐安县| 常宁市| 历史| 墨竹工卡县| 肇源县| 儋州市| 广西| 太仓市| 夏邑县| 桃源县| 攀枝花市| 本溪市| 奉节县| 玛沁县| 曲周县|