- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 153字
- 2021-07-02 23:54:30
Extensions
Extensions add new functionality to an existing class, structure, enumeration, or protocol. This includes the ability to extend types for which we do not have access to the original source code.
Extensions in Swift enables us to perform the following:
- Define instance methods and type methods
- Provide new initializers
- Define and use new nested types
- Define subscripts
- Add computed properties and computed static properties
- Make an existing type conform to a new protocol
Extensions enable us to add new functionality to a type, but we will not be able to override the existing functionality.
In the following example, we extend AType by making it conform to two protocols:
extension AType: AProtocol, BProtocol { }
The following example presents an extension to Double by adding computed properties:
extension Double {
var mm: Double{ returnself / 1_000.0 }
var ft: Double{ returnself / 3.2884 }
}
let threeInch = 76.2.mm
let fiveFeet = 5.ft
推薦閱讀
- 大數據技術基礎
- Creating Dynamic UIs with Android Fragments(Second Edition)
- Sybase數據庫在UNIX、Windows上的實施和管理
- INSTANT Apple iBooks How-to
- 數據應用工程:方法論與實踐
- Kubernetes快速進階與實戰
- 領域驅動設計精粹
- 代碼的未來
- Managing Software Requirements the Agile Way
- 大學計算機:理解和運用計算思維
- Hadoop海量數據處理:技術詳解與項目實戰(第2版)
- Reactive Programming in Kotlin
- Hadoop與大數據挖掘
- CORS Essentials
- Spark大數據處理與分析