- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 125字
- 2021-07-02 14:45:00
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)
推薦閱讀
- 數據要素安全流通
- 數據庫原理及應用教程(第4版)(微課版)
- Python絕技:運用Python成為頂級數據工程師
- Libgdx Cross/platform Game Development Cookbook
- Enterprise Integration with WSO2 ESB
- Remote Usability Testing
- 大數據技術原理與應用:概念、存儲、處理、分析與應用
- Access數據庫開發從入門到精通
- 大數據時代系列(套裝9冊)
- 信息融合中估計算法的性能評估
- 從Lucene到Elasticsearch:全文檢索實戰
- 數據中臺實戰:手把手教你搭建數據中臺
- 一本書讀懂大數據
- Access 2010數據庫應用技術教程(第二版)
- 數據庫基礎與應用