Book Image

Protocol-Oriented Programming with Swift

By : Jon Hoffman
Book Image

Protocol-Oriented Programming with Swift

By: Jon Hoffman

Overview of this book

<p>At the heart of Swift's design is an incredibly powerful idea: protocol-oriented programming. Its many benefits include better code maintainability, increased developer productivity and superior application performance. The book will teach the reader how to apply the ideas behind the protocol oriented programing paradigm to improve the code they write.</p> <p>This book will introduce the readers to the world of protocol-oriented programming in Swift and will demonstrate the ideas behind this new programming paradigm with real world examples. In addition to learning the concepts of Protocol Oriented programming, it also shows the reader how to reduce the complexity of their codebase using protocol extensions. Beginning with how to create simple protocols, readers will learn how to extend protocols and also to assign behaviors to them.</p> <p>By the end of this book readers will be able to harness the power of protocol-oriented programming to build real world applications.</p>
Table of Contents (14 chapters)
Protocol-Oriented Programming with Swift
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Preface

This book is about protocol-oriented programming. When Apple announced Swift 2 at the World Wide Developers Conference (WWDC) in 2015, they also declared that Swift was the world's first protocol-oriented programming language. By its name, we may assume that protocol-oriented programming is all about the protocol; however, that would be wrong assumption. Protocol-oriented programming is about so much more than just the protocol; it is actually a new way of not only writing applications, but thinking about programming as well.

One of the biggest misconceptions about protocol-oriented programming is that it is just another name for object-oriented programming. Chapter 1, Object-Oriented and Protocol-Oriented Programming, immediately takes on this myth by comparing protocol-oriented programming to object-oriented programming to see what is similar and what the differences are. We will also discuss the advantages and disadvantages of both programming paradigms.

From Chapter 2, Our Type Choices, to Chapter 5, Let's Extend Some Types, we will take an in-depth look at each of the components of the protocol-oriented programming paradigm. These chapters are designed to give the reader a solid understanding of the different components of protocol-oriented programming, so users will understand how they can use these components in their applications.

The last two chapters are written to help the reader understand how they can design their application in a protocol-oriented programming way. Chapter 6, Adopting Design Patterns in Swift, looks at how we would implement several design patterns in a protocol-oriented way and Chapter 7, Case Studies, looks at three real-world case studies to reinforce everything previously discussed in the book.

What this book covers

Chapter 1, Object-Oriented and Protocol-Oriented Programming, will show the differences between object-oriented programming and protocol-oriented programming, giving the reader a good understanding of the different paradigms. By the end of the chapter, the reader should have a basic understanding of protocol-oriented programming and what it offers.

Chapter 2, Our Type Choices, will look at the different types that Swift offers (structs, classes, enums, and tuples). We will show several examples of when to use the various types and when not to.

Chapter 3, Catching Our Errors, will look at the various ways we can catch and report errors. This chapter will not only cover the new Swift 2 do-try-catch blocks, but also the older error handling patterns.

Chapter 4, All about the Protocol, will be all about the protocol. We will show the advantages of using protocols and why, in our design, we should start with the protocol.

Chapter 5, Let's Extend Some Types, will cover extensions in great detail. We will look at how to extend standard Swift types, such as structures and classes. We will also look at protocol extensions and discuss why they are so important in protocol-oriented programming.

Chapter 6, Adopting Design Patterns in Swift, will be implementing several design patterns using protocol-oriented programming. For each of the design patterns, we will look at the problem they are designed to solve and how to implement the pattern.

Chapter 7, Case Studies, will explore three case studies. This chapter is designed to pull everything from the first six chapters together to show the reader how to use protocol-oriented programming with design patterns in real-world situations.

What you need for this book

To follow along with the examples in this book, the reader will need to have an Apple computer with OS X 10.10 or higher installed. They will also need to install Xcode version 7.0 or higher with Swift version 2 or higher and have at least basic knowledge of the Swift programming language.

The examples, as they are shown in this book, should work with the Linux port of the Swift language however the downloadable code for this book requires Xcode which is only available on the Apple platform.

Who this book is for

This book is for Swift developers who have at least an introductory knowledge of the Swift programming language, who want to learn and implement protocol-oriented programming in their real-world applications. It is also for developers who not only want to understand protocol-oriented programming, but also want to fully understand the different components of the programming paradigm. This book is written for developers who learn best by looking at and working with code, because every concept covered in the book is backed by example code written to give the reader a solid understanding of the current topic and to demonstrate how to properly implement it.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

class Jolt {
    var volume: Double
    var caffeine: Double
    var temperature: Double
    var canSize: Double
    var description: String

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The answer to this question is polymorphism.".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.