- 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.
推薦閱讀
- GitHub Essentials
- 在你身邊為你設計Ⅲ:騰訊服務設計思維與實戰(zhàn)
- 企業(yè)數(shù)字化創(chuàng)新引擎:企業(yè)級PaaS平臺HZERO
- 有趣的二進制:軟件安全與逆向分析
- 醫(yī)療大數(shù)據(jù)挖掘與可視化
- iOS and OS X Network Programming Cookbook
- Learn Unity ML-Agents:Fundamentals of Unity Machine Learning
- ZeroMQ
- Oracle PL/SQL實例精解(原書第5版)
- 菜鳥學SPSS數(shù)據(jù)分析
- 區(qū)塊鏈+:落地場景與應用實戰(zhàn)
- R Machine Learning Essentials
- 數(shù)據(jù)挖掘競賽實戰(zhàn):方法與案例
- 大數(shù)據(jù)數(shù)學基礎(R語言描述)
- 大數(shù)據(jù)時代系列(套裝9冊)