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 composition


Protocol composition lets our types adopt multiple protocols. This is a major advantage that we get when we use protocols rather than a class hierarchy because classes, in Swift and other single-inheritance languages, can only inherit from one superclass. The syntax for protocol composition is the same as the protocol inheritance that we just saw. The following example shows how to do protocol composition:

struct MyStruct: ProtocolOne, ProtocolTwo, Protocolthree { 
    // implementation here 
} 

Protocol composition allows us to break our requirements into many smaller components rather than inheriting all requirements from a single superclass or class hierarchy. This allows our type families to grow in width rather than height, which means we avoid creating bloated types that contain requirements that are not needed. Protocol composition may seem like a very simple concept, but it is a concept that is essential to protocol-oriented programming. Let's look...