Protocol in Swift with Practical Examples
Tags: ios, swift
Published: 2018-01-19 12:20:29

---
title: Protocol in Swift with Practical Examples
published: true
description: protocol in swift ios
tags: ios, swift
---
![](https://cdn-images-1.medium.com/max/1600/1*80_c-wJSKSVcTIc4hi7KMQ.jpeg)

A _protocol_ defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be _adopted_ by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to _conform_ to that protocol.

> _So, keeping it simple, a protocol says a struct&nbsp;, class or enum that if you want to be THAT, do THIS, this and This. Example: if you want to be a developer, you have to READ, and WRITE&nbsp;CODE._
### Protocol Creation and&nbsp;Adoption

We can create a `Protocol` like below:

```
protocol SomeProtocol { //protocol definition goes here}
```

Classes, structs, and enums can adopt these protocol by placing protocol’s name after the type’s name, separated by a colon, as part of their definition. Multiple protocols can be listed, and are separated by commas.

```
struct SomeStruct: FirstProtocol, SecondProtocol { //struct definition goes here}
```

If a class has a superclass, list the superclass name before any protocols it adopts, followed by a comma.

```
class SomeClass: SomeSuperClass, FirstProtocol, AnotherProtocol { //class definition goes here}
```

### Example one: Property Requirement

{% gist https://gist.github.com/agoiabel/144a287eddbe997745017858f4e70261 %}

The above protocol simply says any class/struct/enum that want to adopt me must have a property called `fullName` which must be a type of `String` and the property must be a _gettable property which was represented with { get }_.

{% gist https://gist.github.com/agoiabel/89651488d788109015ed9781a927824f %}

The `First Implementation` and `Second Implementation` both have a property called `fullName`, hence they both conform to the `FullNameable protocol` though they have different implementation of it. What our `FullNameable` protocol says is that we need to have a `fullName` property, it does not say how we should get this property. Wait, what then makes a property `gettable`?

> A gettable property is a property we can only read from after we have given it an initial&nbsp;value.

{% gist https://gist.github.com/agoiabel/76571a389f78ef34e840e4f7882ccc2e %}

When we try to assign a new value to the `fullName` property, it will fail simply because of the `let` keyword we used when initializing the Lecturer struct.

The reason for this example is to:

- show how to use a protocol and conform to it
- let us understand that classes that conform to a property does not have to have the same implementation.
- Understand what `{ get }` stands for

### Example Two: Connecting Loose End&nbsp;Class

Let us assume we want to make a smoothie and the requirements needed to make the smoothie are `Milk` and `Dairy` products. The make a smoothie, we have to blend some fruits, as well as some Dairy. So we are going to need a blend method that all the classes that will make a smoothie must have, hence we have to enforce this by using protocol like below

{% gist https://gist.github.com/agoiabel/f456c3cc0162c4ce2f6dc272d98d35a7 %}

To make the smoothie, we need to call the blend method on each classes that conform to the Blendable protocol and we can achieve

{% gist https://gist.github.com/agoiabel/62c23d30fdca7e968329c5400b3f0e4e %}

The makeSmoothie method will accept an array of classes that conform to the Blendable protocol, then use the for loop to access the blend method in each class. The application will throw an error if a class that does not conform to the Blendable protocol is passed in as an array.

The reason for this example is to:

- show how to use method as protocol requirement
- Understand how to access loose end class that conform to a protocol

### Example Three: When to use a&nbsp;protocol

A problem is to decide when to use a `protocol` to `class inheritance`. The best cheat to determine when to use a `protocol` compare to `class inheritance` is to know when the type of relationship that determine between your classes. Is it an `IS-A` relationship or a `HAS-A`.

`IS-A` relationship is a type of relationship where a child will have every thing the parent has as well as have additional properties. A good example is an Airplane and a JetPlane. A `JetPlane is basically an Airplane` with additional properties / methods

{% gist https://gist.github.com/agoiabel/b74ab6a61b1acfde0712c8b7e18f24c8 %}

`HAS-A` relationship is a type of relationship where two classes are not the same but share similar properties / method. A good example is a Bird and an Airplane. A Bird can fly as well as an Airplane. This is the best time to use a protocol.

{% gist https://gist.github.com/agoiabel/28862b8aefb03941cd3a0dc62c1d2ac2 %}

The reason for this example is to:

- show when to use protocol over class inheritance

### Example Four: Protocol Inheritance

A protocol can inherit one or more other protocols and can add further requirements on top of the requirements it inherits. The syntax for protocol inheritance is similar to the syntax for class inheritance, but with the option to list multiple inherited protocols, separated by commas:

{% gist https://gist.github.com/agoiabel/1e342c681fa10359dbb0b6538f071bbd %}

Any class/struct /enum that inherit the Pet protocol must conform to the Animal protocol as well because the Pet protocol inherit from the Animal protocol.

I am happy to share this article with you. If you’ve enjoyed this article, do show support by loving it . Thanks for your time and make sure to follow me or drop your comment below. You can read more about Protocol here👇
[here](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html)