Protocols In Swift

You must have learned about interfaces in different programming languages. Similarly, we have protocols in Swift.

Protocol is a blueprint of methods and properties and in order to use the properties and methods that a protocol contains, that specific class will have to inherit the protocol.

In other words, in protocols, we declare methods and properties. The class which inherits the protocol provides definition to those methods and properties.

Protocols can be inherited by class, structure, or enumeration.

So let's understand and implement protocols by creating a simple example.

Prerequisites

  1. MacOS
  2. XCode

Open XCode and create a new Project. Select macOS -> Command  Line Tool.

NOTE
I'm using XCode 13.1. If you have older versions of XCode you can create a Playground project.

Protocols in Swift

Enter your Project Name and choose the language as Swift. Select the desired location where you would like to save your project and click on Create.

Protocols in Swift

Once your project is created, you can see a 'main' file in it. This is where we will be implementing our code.

Now, we will create a Protocol.  

//creating a protocol
protocol Add {
    //declaring a function
    func addition(num1: Int,num2: Int)
}

In the above code snippet, we created a protocol 'Add' which contains the declaration of a function addition in it.

Now, we will create a class which will conform the protocol. Any type which satisfies the requirements of a protocol is said to conform that protocol.

//class inheriting the protocol
class Numbers : Add {
    //implementing the method of the protocol
    func addition(num1: Int, num2: Int) {
        print("Addition of the numbers is : ",num1+num2)
    }
}

The above class 'Numbers' inherits the Add protocol. This is represented by the line 'class Numbers : Add'. As we can see in the above code snippet, the class provides definition of the function of the protocol.

Now we will create an object of the class Numbers.

//creating an object
var numbers = Numbers()
numbers.addition(num1: 5, num2: 4)

The below image shows that as the class has adopted the protocol, the object of the class Numbers is able to access the function of the protocol.

Protocols in Swift 

Now, run the code and observe the output.

Protocols in Swift

Implementing Multiple Protocols

Similarly, multiple protocols can also be created and adopted by a single class.

So let's create some more protocols and the Numbers class will inherit these protocols.

protocol Subtract {
    func subtraction(num1: Int,num2: Int)
}

protocol Multiply {
    func multiplication(num1: Int,num2: Int)
}

protocol Divide {
    func division(num1: Int,num2: Int)
}

Now, let's make the class Numbers conform these protocols too.

//class inheriting multiple protocols
class Numbers : Add,Subtract,Multiply,Divide {
    func addition(num1: Int, num2: Int) {
        print("Addition of the numbers is : ",num1+num2)
    }
    
    func subtraction(num1: Int, num2: Int) {
        print("Subtraction of the numbers is :",num1-num2)
    }
    
    func multiplication(num1: Int, num2: Int) {
        print("Multiplication of the numbers is :",num1*num2)
    }
    
    func division(num1: Int,num2: Int) {
        print("Division of the numbers is :",num1/num2)
    }
}

Now, the object numbers will be able to access the functions of the other protocols too.

//creating an object
var numbers = Numbers()
let no1 = 5
let no2 = 4
numbers.addition(num1: no1,num2: no2)
numbers.subtraction(num1: no1, num2: no2)
numbers.multiplication(num1: no1, num2: no2)
numbers.division(num1: no1,num2: no2)

Run the project and observe the output.

Protocols in Swift

Extending A Protocol

Before learning how to extend a protocol, first let us understand what is an extension.

Suppose you already have an implemented protocol and you want to add some other functionality to that protocol, instead of changing the whole code in the protocol you can just create its extension and add the new functionalities in the extension. In this way, your existing protocol will remain untouched and the new functionalities can also be implemented. This reduces code complexity as well as saves your time and energy.

So let's create an example of extension.

  • We already have created a protocol 'Add' in our project. Now suppose, I have a requirement where in some cases I need to perform addition of 2 numbers and in some other cases I need to perform addition of 3 numbers.

So instead of modifying my existing code, I will simply create an extension of the Add protocol as shown below,

//extension of Add protocol
extension Add {
    func additionOf3Numbers(num1: Int,num2: Int,num3: Int){
        print("Addition of 3 numbers:", num1+num2+num3)
    }
}

Once, you create an extension, your object 'numbers' will be able to access this newly created function implemented in the extension too.

//function implemented in extension
let no3 = 7
numbers.additionOf3Numbers(num1: no1, num2: no2, num3: no3)

Run the code and observe the output. 

Protocols in Swift

Below is the entire code for your reference:

import Foundation

//creating a protocol
protocol Add {
    //declaring a function
    func addition(num1: Int,num2: Int)
}

protocol Subtract {
    func subtraction(num1: Int,num2: Int)
}

protocol Multiply {
    func multiplication(num1: Int,num2: Int)
}

protocol Divide {
    func division(num1: Int,num2: Int)
}

//class inheriting multiple protocols
class Numbers : Add,Subtract,Multiply,Divide {
    func addition(num1: Int, num2: Int) {
        print("Addition of the numbers is : ",num1+num2)
    }
    
    func subtraction(num1: Int, num2: Int) {
        print("Subtraction of the numbers is :",num1-num2)
    }
    
    func multiplication(num1: Int, num2: Int) {
        print("Multiplication of the numbers is :",num1*num2)
    }
    
    func division(num1: Int,num2: Int) {
        print("Division of the numbers is :",num1/num2)
    }
}

//extension of Add protocol
extension Add {
    func additionOf3Numbers(num1: Int,num2: Int,num3: Int){
        print("Addition of 3 numbers:", num1+num2+num3)
    }
}

//creating an object
var numbers = Numbers()
let no1 = 5
let no2 = 4
numbers.addition(num1: no1,num2: no2)
numbers.subtraction(num1: no1, num2: no2)
numbers.multiplication(num1: no1, num2: no2)
numbers.division(num1: no1,num2: no2)

//function implemented in extension
let no3 = 7
numbers.additionOf3Numbers(num1: no1, num2: no2, num3: no3)

I hope you understood what are protocols, how to implement and extend them through this article. Do let me know if you have any doubts or suggestions.

Happy Coding!


Similar Articles