Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Swift Protocol-Oriented Programming - Fourth Edition

You're reading from  Swift Protocol-Oriented Programming - Fourth Edition

Product type Book
Published in Jun 2019
Publisher
ISBN-13 9781789349023
Pages 224 pages
Edition 4th Edition
Languages
Author (1):
Jon Hoffman Jon Hoffman
Profile icon Jon Hoffman

Table of Contents (11) Chapters

Preface 1. Starting with the Protocol 2. Our Type Choices 3. Extensions 4. Generics 5. Memory Management 6. Object-Oriented Programming 7. Protocol-Oriented Programming 8. Adopting Design Patterns in Swift 9. Case Studies 10. Other Books You May Enjoy

Using protocols as a type

Even though no functionality is implemented in a protocol, they are still considered a full-fledged type in the Swift programming language, and can mostly be used like any other type. What this means is that we can use protocols as parameters or return types for a function. We can also use them as the type for variables, constants, and collections. Let's look at some examples of this. For the next few examples, we will use the following Person protocol:

protocol Person { 
    var firstName: String {get set}  
    var lastName: String {get set}  
    var birthDate: Date {get set}  
    var profession: String {get} 
    init (firstName: String, lastName: String, birthDate: Date) 
} 

In this Person protocol, we define four properties and one initializer.

For this first example, we will show you how to use a protocol as a parameter and return type for a function, method, or initializer. Within the function itself, we also use Person as the type for a variable:

func updatePerson(person: Person) -> Person  { 
    var newPerson: Person 
    // Code to update person goes here  
    return newPerson 
} 

We can also use protocols as the type to store in a collection, as shown in the following example:

var personArray = [Person]() 
var personDict = [String:  Person]()  

We can use the instance of any type that conforms to our protocol anywhere that the protocol type is required. Let's assume that we have two types named SwiftProgrammer and FootballPlayer that conform to the Person protocol. We can then use them as follows:

var myPerson: Person 
 
myPerson = SwiftProgrammer(firstName: "Jon", lastName: "Hoffman", 
birthDate: birthDateProgrammer) myPerson = FootballPlayer(firstName: "Dan", lastName: "Marino",
birthdate: birthDatePlayer)

As we saw earlier, we can use the Person protocol as the type for an array, which means that we can populate the array with instances of any type that conforms to the Person protocol. The following is an example of this (note that the bDateProgrammer and bDatePlayer variables are instances of the date type that would represent the birth date of the individual):

var programmer = SwiftProgrammer(firstName: "Jon", lastName: "Hoffman", 
birthDate: bDateProgrammer) var player = FootballPlayer(firstName: "Dan", lastName: "Marino",
birthDate: bDatePlayer) var people: [Person] = [] people.append(programmer) people.append(player)

What we are seeing in these last couple of examples is a form of polymorphism. To use protocols to their fullest potential, we need to understand what polymorphism is.

You have been reading a chapter from
Swift Protocol-Oriented Programming - Fourth Edition
Published in: Jun 2019 Publisher: ISBN-13: 9781789349023
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}