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

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)
主站蜘蛛池模板: 安庆市| 乌拉特后旗| 奉贤区| 资源县| 建平县| 霸州市| 集贤县| 淳化县| 大足县| 扎赉特旗| 阿拉善右旗| 皋兰县| 吐鲁番市| 墨江| 且末县| 都江堰市| 临清市| 子洲县| 深州市| 赫章县| 温泉县| 镇坪县| 长武县| 宜君县| 呼图壁县| 洪湖市| 桐柏县| 桑植县| 东丰县| 湄潭县| 永德县| 阿拉善左旗| 伊川县| 都兰县| 莱阳市| 深圳市| 左权县| 肇州县| 龙井市| 城口县| 长宁区|