Protocol — The power of Swift

Part-2 — conforming a protocol

Hitendra Solanki
Swift India
4 min readMay 18, 2018

--

Updated on 29th March 2019, 4:42 AM GMT 5:30+

Welcome to the 2nd part of this series, In-case you missed first part I will strongly suggest you that first coverup the essential concepts from
Part-1: What is type-casting and class-types?

After completing part-1,
Now you are ready with essential skills to understand this blog.

What is the role of Protocol in type-casting?

Like possible class-types of instance in context of inheritance, when you conform a protocol to your class, the type of conformed protocol is also added in the list of possible class-types for the conformer class.

Instead of saying it class-type now I will say it data-type or simply type, because now we have two super-powerful concepts to play with, Class and Protocol. Sounds better, Isn’t it?

In above example, class Person conforms protocol Displayable

Conform a protocol means your class implements that protocol. This means your class is signing a bond to fullfill all the requirements of a protocol.

Let’s make it more simple,

Conforming a protocol is a wedding of your class and the protocol where your class will treat the protocol as wife and full-fill all her needs without any failure, if you fail to full-fill requirements of protocol, swift compiler will give you error, wife of your class got angry.

Wait a movement please, let me tell you what I understand from your explanation,

Our class Person conforms the protocol Displayable. Person is not ful-filling needs of his wife Displayable, right?

Right, Person is not started full-filling needs of his wife Displayable because this is just newly married couple and she not demanded anything yet. Let’s wait and watch, what will be demands of wife Displayable?

In first example I have conformed a protocol Displayable during implementation of class Person. If you compare it with above example, protocol is conformed with an extension of class.

time please, I have two questions here,

1. What is class extension?
2. Why do you conformed that protocol using class extension?

Class extensions are used to add new functionality in a class without modifying the original implementation of the class. Extensions improves the readability of your code. If your class have multiple responsibilities, you can create groups of related functionality by creating multiple extensions.

Consider you are creating social media application like LinkedIn where Person can manage own profile, create groups, write articles and so on.
You can create three extensions for the class Person to manage your code properly for related functionality. One extension for profile management methods, second extension for LinkedIn groups management methods, third extension for articles management methods of the person.

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. — Martin Fowler

Here we have implemented the protocol Displayable as an extension of the class Person just to improve the readability and scalability of code. So anyone who look at your code can have clear idea what your extension is doing. In future if you required to implement another protocol you can simply create a new extension of the class. Check below example for more clear idea.

Multiple class extension having separate protocols

You can also implement two protocol in a single extension.
Think about the use-case of UITableView, most of the time we implements the UITableViewDelegate and UITableViewDatasource protocols at a same time, if we implement both protocols in a single extension that is okay because after all both protocols are related to functionality of tableview only.

Every time I create UITableView in viewcontroller, I need UITableViewDelegate and UITableViewDataSource both at a same time.
Do you have any solution where I just needs to write a single protocol name only and that is equivalent to both mentioned protocols?

My question is inspired by your swift challenge published on medium about code reduction and reusability.
Thanks for challenging the community of Swifty developers like me, I am still finding a solution of your challenge, but it gave me a new VISION of code reusability @extreamLevel.

Glad to know that I am a one of the reason behind your inspiration.

Now it’s time to answer your question.

To be Continue…
Part-3 — Protocol composition

--

--

Hitendra Solanki
Swift India

Software Engineer, {Self-taught Developer by passion, Quick Learner, Mentor, blogger, TeamLead} GitHub: https://github.com/hitendradeveloper