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

Polymorphism with protocols

The word polymorphism comes from the Greek roots poly (meaning many) and morphe (meaning form). In programming languages, polymorphism is a single interface for multiple types (many forms). There are two reasons to learn the meaning of the word polymorphism. The first reason is that using such a fancy word can make you sound very intelligent in casual conversation. The second reason is that polymorphism provides one of the most useful programming techniques not only in object-oriented programming, but also in protocol-oriented programming.

Polymorphism lets us interact with multiple types through a single uniform interface. In the object-oriented programming world, the single uniform interface usually comes from a superclass, while in the protocol-oriented programming world, that single interface usually comes from a protocol.

In the previous section, we saw two examples of polymorphism with Swift. The first example was as follows:

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

In this example, we had a single variable of the Person type. Polymorphism allowed us to set the variable to instances of any type that conforms to the Person protocol, such as the SwiftProgrammer or FootballPlayer types.

The other example of polymorphism was as follows:

 
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)

In this example, we created an array of Person types. Polymorphism allowed us to add instances of any types that conform to the Person protocol to this array.

When we access an instance of a type through a single uniform interface, as we just saw, we are unable to access type-specific functionality. As an example, if we had a property in the FootballPlayer type that records the age of the player, we would be unable to access that property because it is not defined in the People protocol.

If we do need to access type-specific functionality, we can use type casting.

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}