Protocol Oriented Programming in iOS Swift
Swift supports protocol-oriented programming in iOS applications by using protocols that are a fundamental feature of the swift programming language. Protocol plays a vital role in the structure of Swift Standard Library. However, protocols are used to define the blueprint of methods, properties, and other main functionalities required to complete a particular task. It provides a way to achieve abstraction in the Swift programming language. In simple words, we can say that protocol is like an interface in some other programming languages.
In this article, we will discuss one of the fundamental programming paradigms in swift, I.e., protocol-oriented programming. Here, we will discuss the differences between object-oriented and protocol-oriented programming, including the default implementations of protocols in swift.
What is Protocol-Oriented Programming?
By using protocol-oriented programming, we can group similar methods, functions, and properties. We can specify the protocols for class, Enum, and struct types. However, only class types can use inheritance functionality.
Protocol-oriented programming comes into the picture when our class needs to inherit from two different courses, I.e., the class needs to have the properties of two different classes. Here, we can define protocols that provide an advantage that our class can conform to multiple protocols.
This makes our code more modular as protocols can be seen as basic building blocks of functionality. If we need to add some new functionality in the application, instead of building a whole new class that implements the functionality, we can add different building blocks to our class and define the blocks in our main class accordingly.
Let's create two protocols using the following code.
The above code defines the two protocols as Bank and Company. If we have used the Object-Oriented paradigm to implement this, we could have used Company as a base class and Bank as a child class. However, in a protocol-oriented paradigm, we use everything as a protocol. This allows us to encapsulate the functional concepts without using the base class.
Protocol Conforming type
Let's define the following struct that conforms to our protocols Bank and Company.
This code defines a new struct as RBI that conforms to our protocols, i.e., Bank and Company. RBI has its specified rate of interest for the customers.
Now, let's add the following struct, which is a company but doesn't contain the properties of a Bank.
We can define functional components using protocols and use any relevant object which can conform to them. Now, let's try extending protocols that allow us to define a protocol's default behavior.
Now, we can remove the rateOfInterest from the RBI struct. If we build the playground again, we will notice that the playground will still build successfully since protocol extensions handle that requirement.
Conforming Protocols with Enum
Overriding Default Behavior
Now, we must notice that the Enum Banks automatically receive an implementation for isRegistered property by conforming to the Bank protocol. We can override the default behavior of Bank protocol by using the following code.
Now, only .RBI will return true for isRegistered property if we print the isRegistered property for .SBI and .PNB, it will print false for both.
This is the way to override properties and methods like we override virtual methods in object-oriented programming.
We can also conform our class to the protocols for using protocol functionalities and properties. In the following code, our protocol Drawable is able to draw something on the screen. Our protocol Circle has radius property where our Main class conforms to the Circle protocol to override the radius property for setting a new radius. Our class also conforms to the Drawable for providing the definition of the draw() method.
"Drawing Circle with 10.2 radius"
One of the features of Swift Protocols is how we can compare two objects for equality, greater or lesser values. Let's implement the following code to compare the protocol objects.