To continue the series of articles about Swift, today ArrowHiTech will introduce Swift protocol for you. This article devices into 4 main parts and all of them are very useful when bringing a lot of information about protocol. Please don’t miss them, if not you will regret! So, let’s explore with us right now!
What is Swift protocol?
Before coming to Swift protocol, we should have a glance at a simple example below:
This protocol is called Edible, and we’ll use it to refer to items that can be consumed, such as food.
Not as similar as a class, Protocol has a big difference compared to class. This is because it doesn’t focus on implementing functions and properties yet only describe them.
Above all, a Swift Protocol is a collection of methods, properties, and other specifications that are tailored to a specific task or piece of functionality.
What special benefits Swift protocol has?
First of all, Protocol has the advantage of formalizing connections between various sections of your code without requiring implementations. As a result, you don’t need to tightly couple the components of your code yet you still create rigid structures in it.
How does it activate?
Let’s experience through this program below:
This program above begins with a simple class named Person and it has one property name of type String.Besides, its function is provideSnack(withItem:).
It includes a function that has a parameter item according to the type Apple integrated in the provideSnack(withItem:.
Let’s start using those Person and Apple classes like this program below:
You should remember that withItem part is the argument label in the function provideSnack(withItem:). The parameter name item can be used within the function, and the statement mark withItem can be used outside of the function. Hence, this improves the function’s descriptiveness.
However, it has a problem with Person class. It’s just allowed to eat apples! Worse, the Person and Apple classes have become tightly-coupled. This is because they depend on each other’s implementation to work properly. So, is there an effective method to get around this difficulty? Then, Swift Protocol will resolve it.
What Swift protocol can do?
The first thing we should do is change the provideSnack(withItem:) function.
Instead of using Apple like before, now, we replace by the type of the item parameter called Edible. Therefore, we’ve mentioned that the item can be of any kind as long as it complies with Edible.
For instance, let’s try with another food by using CandyBar:
Take a look at this program above and see how this CandyBar class follows Edible’s guidelines? The provideSnack(withItem:) function can easily accept an instance of that class. As an example below:
So, what really is going on here?
- On top of that, in this case, we’ve simply added the Edible protocol as a prerequisite for the provideSnack(withItem:) function, rather than coupling the Person and Apple classes.
- Secondly, only need suitable with Edible, you can absolutely provide any item you want with the provideSnack(withItem:) function.
- Finally, you can call the eat() function on item without knowing its exact form since the Swift protocol specifies it.
Types of Swift protocol
In Swift, protocols are full-fledged varieties. As a result, you can use protocols in a variety of settings as long as this place permits other types. This example below will demonstrate for you:
In fact, an array can contain any type such as Int or String. Since a protocol is also a type, you can use a protocol as the array’s type. As a result, this will increase the flexibility of your code yet still keep the ability to formalize your code’s structure.
Following that, you can use Swift protocol in a lot of places. Above are some of them:
- Firstly, as a parameter type or return type in a function
- Secondly, as the type of a constant, variable, or property
- Thirdly, as the type of items in an array, dictionary, or other collections.
How Swift protocol is practical in reality?
Let’s make an instance in Practical iOS development. Firstly, imagine that we are both working on the same restaurant app. On this app, I take charge of designing the kitchen and you build the tables, waiters, and customers.
In case I need to give the waiter a cake that has been prepared in the kitchen. Since you’re constructing the waiter, you have to get information from me about the types of foods I’ll be providing.
However, the kitchen not only includes only cake, but it also serves many types of food such as: pizza, beef, drinks and so on.
As a result, we devise a complex structure that the waiter can use. Especially, we use subclassing and inheritance. However, we soon learn that a cake and a beer cannot both be superclasses. This is because there is a big difference between them.
Besides, we also want to keep the waiters lean. Then, they don’t need to rely on the kitchen’s inner workings. With waiters, we understand that the only thing they want to know is whether a dish from the kitchen can be taken from the kitchen to the table.
Following that, a Servable can be made like:
Thanks to that, the waiters now realize they must serve anything that comes from the kitchen to customers. They’ve specified in their own code that everything prepared by the kitchen must follow the Servable protocol.
Above all, we should collaborate on our own kitchen and restaurant implementations. We’ve just formalized one rule: How the kitchen and wait staff connect to each other? That’s all they’ll ever need to know! Not only that, a waiter can serve food to customers even if the kitchen adjusts the way to serve.
All in all, ArrowHiTech hopes this blog is helpful for you. Swift is a quite new programming language and maybe in some cases, you can feel difficult with it. Then, if there is any problem about Swift protocol that you can’t resolve, you can ask us for help. With many years experience in Web/mobile app development, we surely the results will make you satisfied a lot.