Why you should be using Protocols.

Austin Beck
CodeX
Published in
3 min readJun 9, 2021
Photo by Alex Bachor on Unsplash

As an iOS developer, you have likely already encountered protocols in either projects you have made, or other projects you have looked at. But why do developers use protocols? What exactly do they do and why are they useful? Today we are going to dive into protocols and learn how to use them in your next Swift project!

Protocols are used to create a core structure for an object in Swift. This structure is similar to that of a class, but one of the main differences is that you only define what functions are associated with a protocol, not the functions themselves. For a better understanding, take a look at the example below:

protocol Routine {
wakeUp()
getCoffee()
goToWork()
}

The example above creates a protocol named “Routine”. Within this protocol we have stated that any object that will be of type “Routine” must also have the wakeUp, getCoffee, and goToWork functions within them. As you can see from this protocol declaration, we are not creating the functions themselves like we would in a class, we are simply telling Swift “Hey there are going to be objects of type Routine that will have at least these three functions in them”.

You can see an example of how we would use the protocol created above in our code:

class MondayRoutine: Routine {
func wakeUp() {
print("...turning off alarm")
}
func getCoffee() {
print("Grabbing a cup of coffee")
}
func goToWork() {
print("Driving to work")
}
}

In this example, we have created a class called “Monday Routine” that conforms to the “Routine” protocol. In order for this class to conform to the “Routine” protocol, we must include the three functions required within this protocol. Within the class itself, we have created the function body unlike within the protocol.

If we wanted to create another class that conforms to the “Routine” protocol, we could do so like below:

class TuesdayRoutine: Routine {
func wakeUp() {
print("...overslept alarm")
}
func getCoffee() {
print("Pour two cups of coffee")
}
func goToWork() {
print("Take the bus to work")
}
}

We have now created another class that conforms to the “Routine” protocol. As you can see, TuesdayRoutine is going to print out different statements than MondayRoutine, but they are both a Routine as they include the wakeUp, getCoffee, and goToWork functions.

So why use protocols? Protocols are extremely useful to formalize the connection between your code without providing implementation. They allow us to provide structure within our code without closely tying different system components together.

If you are building out a large project and know that you are going to need four different functions for a specific type of class that you will be creating multiple times, this would be a great place to start implementing protocols. It allows great reusability within your code. Protocols also allow us the peace of mind that any class we have created must have at least the functions we have declared within the protocol declaration itself.

Another great thing to know about protocols is that they are a Swift type. This means that you can use them just like you would other types in Swift. You can see an example of this below:

func weeklyRoutine(days: [Routine]) {
for day in days {
day.wakeUp()
day.getCoffee()
day.goToWork()
}
}

This example creates a function that takes an array of Routine type objects. The function then iterates through the array and calls each of the functions that we know are included in our days since they are of type Routine.

Hopefully after reading this article you have a basic understanding of Protocols in Swift and how they can be useful within your code. I encourage you to play around with Protocols in your next project and see how they can benefit you!

--

--