- 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.
- 手機安全和可信應用開發指南:TrustZone與OP-TEE技術詳解
- .NET 4.0面向對象編程漫談:基礎篇
- OpenCV for Secret Agents
- INSTANT CakePHP Starter
- Python神經網絡項目實戰
- Android 應用案例開發大全(第3版)
- Building Minecraft Server Modifications
- Symfony2 Essentials
- Java系統化項目開發教程
- MySQL從入門到精通(軟件開發視頻大講堂)
- D3.js By Example
- 大話Java:程序設計從入門到精通
- Vue.js光速入門及企業項目開發實戰
- Android編程權威指南(第4版)
- Android技術內幕(系統卷)