Protocol-Oriented Programming in Swift

Protocol-Oriented Programming in Swift

Swift – the First POP Language

At WWDC 2015, Apple announced that Swift is the world’s first Protocol-Oriented Programming (POP) language.

So What’s POP?

Protocol-Oriented Programming is a new programming paradigm ushered in by Swift 2.0. In the Protocol-Oriented approach, we start designing our system by defining protocols. We rely on new concepts: protocol extensions, protocol inheritance, and protocol compositions.

The paradigm also changes how we view semantics. In Swift, value types are preferred over classes. However, object-oriented concepts don’t work well with structs and enums: a struct cannot inherit from another struct, neither can an enum inherit from another enum. So inheritance – one of the fundamental object-oriented concepts – cannot be applied to value types.

On the other hand, value types can inherit from protocols, even multiple protocols. Thus, with POP, value types have become first class citizens in Swift.

Start with a Protocol

When designing a software system, we try to identify the elements needed to satisfy the requirements of a given system. We then model the relationships between these elements. We can start with a superclass and model its relationships through inheritance. Or we can start with a protocol and model the relationship as a protocol implementation. Swift provides full support for both interpretations. However, Apple tells us:

“Don’t start with a class, start with a protocol.”

Table of Contents

Continue reading this article on hack.guides() by Pluralsight

About the Author:

Károly Nyisztor is a veteran mobile developer and instructor. He has built several successful iOS apps and games—most of which were featured by Apple—and is the founder at LEAKKA, a software development, and tech consulting company. He's worked with companies such as Apple, Siemens, SAP, and Zen Studios. Currently, he spends most of his days as a professional software engineer and IT architect. In addition, he teaches object-oriented software design, iOS, Swift, Objective-C, and UML. As an instructor, his aim is to share his 20+ years of software development expertise and change the lives of students throughout the world. He's passionate about helping people reveal hidden talents, and guide them into the world of startups and programming. You can find his courses and books on all major platforms including Amazon, Lynda, LinkedIn Learning, Pluralsight, Udemy, and iTunes.

One Comment

  1. carlos July 23, 2017 at 2:12 pm - Reply

    Yay! My article is being featured on hack.guides() by Pluralsight

Leave A Comment