- Swift Functional Programming(Second Edition)
- Dr. Fatih Nayebi
- 111字
- 2021-07-02 23:54:30
Protocol extensions
Protocol extensions allow us to define behavior on protocols rather than in each type's inpidual conformance or global function. By creating an extension on a protocol, all conforming types automatically gain this method implementation without any additional modification. We can specify constraints that conforming types must satisfy before the methods and properties of the extensions are available when we define a protocol extension. For instance, we can extend our ExampleProtocol to provide default functionality as follows:
extension ExampleProtocol {
var simpleDescription: String {
get {
return "The description is: \(self)"
}
set {
self.simpleDescription = newValue
}
}
mutating func adjust() {
self.simpleDescription = "adjusted simple description"
}
}
推薦閱讀
- PyTorch深度學習實戰:從新手小白到數據科學家
- SQL Server 2012數據庫技術與應用(微課版)
- Python廣告數據挖掘與分析實戰
- 數據庫原理與應用(Oracle版)
- Spark大數據編程實用教程
- Starling Game Development Essentials
- SQL優化最佳實踐:構建高效率Oracle數據庫的方法與技巧
- 數據庫原理與設計(第2版)
- 跨領域信息交換方法與技術(第二版)
- 區塊鏈技術應用與實踐案例
- 貫通SQL Server 2008數據庫系統開發
- Internet of Things with Python
- 數據庫原理及應用:SQL Server 2016
- 工業大數據融合體系結構與關鍵技術
- 大數據計算系統原理、技術與應用