Protocol Oriented Programming in Swift — Keeping it simple.

Abhishek Dwivedi
CodeX
Published in
3 min readMar 29, 2022

Abstraction has always played a prominent role in having a modularised code. A code which abstracts all the complexities and shows only what is needed. Swift calls it Protocols, what other popular languages call Interfaces.

Protocol oriented programming in Swift was introduced by Apple in WWDC 2015 where they said —

Don’t start with a class, start with a Protocol.

It basically defined the approach for Protocol oriented programming. — First, define the capabilities, then, create types which adopt those capabilities.

Apple also defined the naming convention for Protocols — Protocols should start with a capital letter, and either describe what it is, with a noun, or describe what it does, with a noun followed by a suffix. An example for the former is UITableViewDelegate, and an example of the latter is Codable.

That’s enough of the context we will need at this stage. Let’s jump to coding.

Defining a Protocol — Define behaviour of a Protocol.

In the below code, we first define a protocol, named Flyable. It basically defines a flying behaviour which any type can adopt in our application.

The Flyable protocol defines speed and maxSpeed as attributes (properties) and fly, flyWithSpeed and land as capabilities (functions) which any flying object can adopt.

The speed property defined in the below code can be set and read, but the maxSpeed property can only be read. It is basically a read only property. This explains the get set and get definitions next to the property name.

Conforming to Protocols — Adopting the behaviour of a Protocol.

In the below code, Bird adopts the flying behaviour by conforming to the Protocol, Flyable. Any bird that conforms to the Flyable protocol, must define the properties and functions which the Protocol has defined.

Extending a protocol — Defining default behaviour of Protocols.

In the below code, we extend the protocol, Flyable to define its default implementation. We provide a default maxSpeed as 20 and also provide default implementations for flyWithSpeed and land functions.

The point to note here is, if we later create another type which conforms to protocol, Flyable, it can modify the default behaviour to have better speed and landing capabilities.

Why protocol, why not Inheritance.

When we discussed about defining a standard behaviour which any type can conform to, the first thing that comes to our mind is inheritance.

Can’t we just create a base class, add a structure to it, and subclasses can inherit and add their implementation ?

Yes, we can. Inheritance is great, but it certainly comes with its own set of limitations.

  1. Like in many programming languages, multiple inheritance is not supported in Swift — If we go by the inheritance approach and we later want to add behaviours to our base class which we think can be useful for all subclasses, the base class will end up having way too much code that it should ideally have. With the approach of Protocols, we can define multiple Protocols for each set of behaviours and the type can conform to multiple Protocols, all at once.
  2. Inheritance only supports class types, not value types — Inheritance is used so subclasses can inherit the behaviour of its base class and have their own behaviour/implementations. But what if we want to do the same with structs ? How can we achieve it ? Protocols comes to the rescue here, which supports both, class and value types.

That was all for the scope of this article.

That is all that we have in the scope of this article. Protocols have a lot more capabilities than what we talked here. This article intends to give an overview of the Protocol Oriented Programming in general in Swift. If you would like to learn more, I highly suggest that you go through the WWDC 2015 session where Apple introduced Protocol Oriented Programming and explained its implementation in Swift.

Hope you found the article useful, please post your questions/feedback in comments, and I will be glad to respond. If you liked the article, you can consider following me on Medium and connect in LinkedIn.

--

--

Abhishek Dwivedi
CodeX
Writer for

Sr. iOS Engineer by profession. Author and trainer by passion.