- 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"
}
}
推薦閱讀
- 同步:秩序如何從混沌中涌現
- Access 2016數據庫教程(微課版·第2版)
- Python數據分析入門:從數據獲取到可視化
- Effective Amazon Machine Learning
- 區塊鏈通俗讀本
- 數據庫系統原理及應用教程(第4版)
- 基于Apache CXF構建SOA應用
- Starling Game Development Essentials
- 數據挖掘原理與SPSS Clementine應用寶典
- 新基建:數據中心創新之路
- INSTANT Apple iBooks How-to
- Oracle數據庫管理、開發與實踐
- 區域云計算和大數據產業發展:浙江樣板
- Web Services Testing with soapUI
- Delphi High Performance