Book Image

Swift 3 Protocol-Oriented Programming - Second Edition

By : Jon Hoffman
Book Image

Swift 3 Protocol-Oriented Programming - Second Edition

By: Jon Hoffman

Overview of this book

<p>One of the most important additions to the new features and capabilities of the Swift programming language was an overhaul of Protocols. Protocol-oriented programming and first class value semantics have now become two incredibly powerful concepts at the heart of Swift’s design.</p> <p>This book will help you understand the difference between object-oriented programming and protocol-oriented programming. It will demonstrate how to work with protocol-oriented programming using real world use cases. You will gain solid knowledge of the different types that can be used in Swift and the differences between value and reference types. You will be taught how to utilize the advanced features of protocol-oriented programming to boost the performance of your applications.</p> <p>By the end of the book, you will have a thorough understanding of protocol-oriented programming and how to utilize it to build powerful, practical applications.</p>
Table of Contents (14 chapters)
Swift 3 Protocol-Oriented Programming - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Protocol syntax


In this section, we will look at how to define a protocol, define requirements within a protocol, and specify that a type conforms to a protocol.

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 state that the MyStruct structure conforms to the MyProtocol protocol:

struct MyStruct: MyProtocol { 
  //structure implementation here 
} 

A type can also conform to multiple protocols. We list...