- Swift 4 Protocol-Oriented Programming(Third Edition)
- Jon Hoffman
- 201字
- 2021-07-08 09:42:35
Defining a protocol
The syntax we use to define a protocol is very similar to the syntax used to define a class, structure, or enumeration. The following example shows the syntax used to define a protocol:
Protocol MyProtocol { //protocol definition here }
To define the protocol, we use the protocol keyword followed by the name of the protocol. We then put the requirements, which our protocol defines, between curly brackets. Custom types can state that they conform to a particular protocol by placing the name of the protocol after the type's name, separated by a colon. The following example shows how we would define that a structure conforms to a protocol:
struct MyStruct: MyProtocol { //structure implementation here }
A type can also conform to multiple protocols. We list the multiple protocols that the type conforms to by separating them with commas:
struct MyStruct: MyProtocol, AnotherProtocol, ThirdProtocol { // Structure implementation here }
Having a type conform to multiple protocols is a very important concept within protocol- oriented programming, as we will see later in the chapter and throughout this book. This concept is known as protocol composition.
Now let's see how we would add property requirements to our protocol.
- Python概率統計
- Learning C# by Developing Games with Unity 2020
- GeoServer Cookbook
- 垃圾回收的算法與實現
- 基于Java技術的Web應用開發
- VSTO開發入門教程
- Learn Swift by Building Applications
- Koa開發:入門、進階與實戰
- Bootstrap 4:Responsive Web Design
- PhpStorm Cookbook
- Visual C++數字圖像處理技術詳解
- 用戶體驗增長:數字化·智能化·綠色化
- OpenStack Orchestration
- Terraform:多云、混合云環境下實現基礎設施即代碼(第2版)
- Internet of Things with ESP8266