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

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)
主站蜘蛛池模板: 五华县| 承德县| 兴城市| 喜德县| 遂平县| 抚远县| 疏勒县| 汕头市| 沭阳县| 布尔津县| 炎陵县| 襄垣县| 贵定县| 长丰县| 宾川县| 汾西县| 南漳县| 牡丹江市| 合水县| 龙江县| 云阳县| 囊谦县| 依兰县| 十堰市| 五莲县| 措勤县| 会东县| 郁南县| 陆良县| 贺州市| 噶尔县| 赫章县| 朝阳市| 广平县| 宜川县| 象州县| 民县| 修武县| 武夷山市| 镇江市| 页游|