- 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
推薦閱讀
- GitHub Essentials
- 數據產品經理高效學習手冊:產品設計、技術常識與機器學習
- Python數據挖掘:入門、進階與實用案例分析
- Java Data Science Cookbook
- DB29forLinux,UNIX,Windows數據庫管理認證指南
- 數據結構與算法(C語言版)
- Oracle高性能自動化運維
- OracleDBA實戰攻略:運維管理、診斷優化、高可用與最佳實踐
- 跨領域信息交換方法與技術(第二版)
- Hands-On Deep Learning for Games
- ECharts數據可視化:入門、實戰與進階
- Learning Ansible
- C# 7 and .NET Core 2.0 High Performance
- 實用預測分析
- Access 2013 數據庫管理與應用從新手到高手