DEV Community

Agoi Abel Adeyemi
Agoi Abel Adeyemi

Posted on • Updated on

Protocol in Swift with Practical Examples

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 , 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 CODE.

Protocol Creation and Adoption

We can create a Protocol like below:

protocol SomeProtocol { //protocol definition goes here}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

Example one: Property Requirement

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 }.

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 value.

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 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

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

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 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

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.

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:

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

Top comments (2)

Collapse
 
twof profile image
Alex Reilly

Protocol Oriented Programming
youtube.com/watch?v=g2LwFZatfTI

Definitely in the top 10 videos every Swift developer should watch.

Collapse
 
abelagoi profile image
Agoi Abel Adeyemi

saw that already but havent watch it. Thanks, I think I should know