- Hands-On Design Patterns with Swift
- Florent Vilmart Giordano Scalzo Sergio De Simone
- 116字
- 2021-07-02 14:45:00
Declaring a protocol
You declare protocols using the protocol keyword, as follows:
protocol Toggling {
mutating func toggle()
}
Now that this protocol has been declared, any time that you declare a type conforming to Toggling, you'll be required to implement a mutating toggle() function.
You can use protocols in your type declarations, method declarations, or variable declarations. While it is technically possible to use protocols as interfaces for your objects or structs, that is usually not how they are used in Swift. Often, you will find yourself conforming to protocols when declaring your custom types or later in your code base, part of extending your existing type to bring additional functionality to it.
推薦閱讀
- Redis使用手冊(cè)
- LibGDX Game Development Essentials
- 云計(jì)算服務(wù)保障體系
- Oracle高性能自動(dòng)化運(yùn)維
- 數(shù)據(jù)革命:大數(shù)據(jù)價(jià)值實(shí)現(xiàn)方法、技術(shù)與案例
- 數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用教程
- gnuplot Cookbook
- Oracle RAC日記
- 數(shù)據(jù)庫(kù)原理與應(yīng)用
- IPython Interactive Computing and Visualization Cookbook(Second Edition)
- Hadoop集群與安全
- Google Cloud Platform for Developers
- 二進(jìn)制分析實(shí)戰(zhàn)
- 智慧城市中的大數(shù)據(jù)分析技術(shù)
- Node.js High Performance