- 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"
}
}
推薦閱讀
- Word 2010中文版完全自學(xué)手冊
- Python數(shù)據(jù)挖掘:入門、進階與實用案例分析
- 使用GitOps實現(xiàn)Kubernetes的持續(xù)部署:模式、流程及工具
- 大數(shù)據(jù)可視化
- Python醫(yī)學(xué)數(shù)據(jù)分析入門
- 網(wǎng)站數(shù)據(jù)庫技術(shù)
- 大數(shù)據(jù)治理與安全:從理論到開源實踐
- Hadoop集群與安全
- 區(qū)塊鏈技術(shù)應(yīng)用與實踐案例
- Access數(shù)據(jù)庫開發(fā)從入門到精通
- Node.js High Performance
- Deep Learning with R for Beginners
- Oracle 內(nèi)核技術(shù)揭密
- 一類智能優(yōu)化算法的改進及應(yīng)用研究
- Machine Learning for Mobile