Table of contents
1.
Introduction
2.
Conform Class To Swift Protocol
2.1.
Swift Protocol To Calculate Area
3.
Conforming Multiple Protocols
4.
Swift Protocol Inheritance
5.
Protocol Extensions
6.
Frequently Asked Questions
6.1.
Is Swift a protocol interface?
6.2.
What do you understand by the Swift protocol class?
6.3.
Why Swift is referred to as pop?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Swift Protocols

Author ANKIT MISHRA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A protocol is a set of methods, attributes, and other customized specifications for a certain task or piece of functionality. A class, structure, or enumeration can then adopt the protocol to offer an actual implementation of those requirements. Any type that meets the protocol's requirements is said to conform to that protocol.

In simple words, A protocol is a set of methods or attributes that classes can use to operate (or any other types).

To define a protocol, we require the protocol keyword.

protocol GreetMessage {
 // this is called the blueprint of a property
 var name: String { get }
 // this is the blueprint of a method
 func text_message()
}    

Here,

GreetMessage - name of the protocol

name - a gettable property

text_message() - method definition without any implementation

Syntax:

protocol SomeProtocolName {
// protocol definition here
}

Conform Class To Swift Protocol

To use a protocol in Swift, other classes must conform it. We must give a real implementation of the method when we conform a class to the protocol.

Here's how to make a class conform to the protocol:

// conform class to get the Greet protocol
class Employee: GreetMessage {


 // implementation of the property
 var name = "Perry"


 // implementation of the method
 func text_message() {
   print("Hello, Good Morning!")
 }
}

Here, we've made the Employee class conform with the Greet protocol. As a result, we must implement the name property and the message() method.

Let’s look at the example of swift protocol,

Swift Protocol To Calculate Area

protocol Quadilateral {


 func getArea(length: Int, breadth: Int)
}


// conform to the Quadrilateral protocol here
class Rectangle: Quadrilateral {


 // implementation of the method
 func getArea(length: Int, breadth: Int) {
   print("Area of the rectangle:", length * breadth)
 }
}


// creating an object
var r1 = Rectangle()


r1.getArea(length:4, breadth: 7)

Output

Area of the rectangle: 28

In the above example, we implemented the Quadrilateral protocol. The protocol comprises of the getArea() method's blueprint, which has two parameters: length and breadth.

The rectangle is a Quadrilateral-based class that provides the actual implementation of the getArea() method.

Conforming Multiple Protocols

In Swift, A class is capable of conforming to multiple protocols. For example,

protocol Addition {
 ...
}


protocol Multiply {
 ...
}


class Calculation: Addition, Multiply {
 ...
}

Here, the class named Calculation conforms to the Addition and Multiply protocols.

Swift Protocol Inheritance

protocol Car {
 ...
}


protocol Brand: Car {
 ...
}

The Car protocol is inherited by the Brand protocol in this case. Now, any class that implements Brand must have implementations for all Car and Brand attributes.

Protocol Extensions

In Swift, we can extend protocols using the extension keyword. For example,

// protocol definition here
protocol Brake {
 func applyBrake()
}


// defining the class that conforms Brake
class Car: Brake {
 var speed: Int = 0


 func applyBrake() {
   print("Brake Applied")
 }
}


// extend the protocol
extension Brake {
 func stop() {
   print("Engine is been Stopped")
 }
}


let car1 = Car()
car1.speed = 75
print("Speed:", car1.speed)


car1.applyBrake()


// access the extended protocol
car1.stop()

Output

Speed: 75
Brake Applied
Engine is been Stopped

In the above shown example, we first created the protocol named0 Brake that defines the function applyBrake().

Later,

We have extended the Brake protocol and defined the stop() function inside it.

// extending the protocol
extension Brake {
 func stop() {
   print("Engine is been Stopped")
 }
}

This sums up our discussion on the swift protocol. Let's move to the FAQ's Section.

Frequently Asked Questions

Is Swift a protocol interface?

Properties that must be implemented can also be specified in Swift protocols (i.e., fields). The mutating keyword is used in Swift protocols to deal with value/reference (because protocols can be implemented by structures, enumerations, or classes). "Protocol Composition" allows you to combine protocols at any time.

What do you understand by the Swift protocol class?

A protocol is a set of methods, attributes, and specifications specialized to a certain activity or piece of functionality. A class, structure, or enumeration can then adopt the protocol to offer an actual implementation of those criteria.

Why Swift is referred to as pop?

Swift is the First POP Language. Apple announced that Swift is the world's first Protocol-Oriented Programming (POP) language.

Conclusion

In this article, we have extensively discussed Swift Protocol. We defined them and discussed various features and attributes of the swift protocol.

To read more about Swift, you can refer here Swift GenericsWhy Swift?Swift Tutorial.

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses, refer to the mock test and problems; look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass