Familiarize yourself with protocol extensions

Tram Ho

Extension

Before we get started, let’s take a look at Swift Extensions. Surely anyone who has code ios using Objective C language will know that there is a similar ideological name with Extensions in Swift. In Objective C, it is known by another name, “Category”. Using Extensions makes it possible to simply add functions to classes, structs, enum or protocols. Note, we cannot extend the class / struct / protocol / enum with a new stored property. You can read more in Extensions

Protocol

Protocol helps us create a blueprint (bluesprint) including functions, properties, in order to use it, the classes / struct / enumeration will need to follow the blueprint. We can easily create a protocol like this:

Above, I have created a protocol called Actions, which represents a blueprint including actions, the scenario here is to create a design that includes actions, which can be used by both people. that animal. Next, I will create a struct called Human, then I will adopt the function goAhead (). as follows:

At this point, the Human will need to follow the Actions blueprint, using the goAhead function. Next, I will also create Animal, and then we will also adopt the design of Actions.

After both Human and Animal have conformed to Actions, I will run the test, now the result is as follows:

Instead of just adopting a protocol, we can adopt many different protocols at the same time. For example, I will add a protocol named Dog:

A dog will bark. And I will create a struct called Golden, it represents the famous Golden dog breed:

At this time, our Golden dog can walk and bark at the same time =))

Protocol extensions

Let’s also review the above example, if our Golden dog does not adopt the bark () method, the system will error.

Error:

We can implicitly understand that any dog ​​will be able to bark, but there will still be the exception of some who are mute.

So how do we still adopt protocol Dog to make sure that all other dogs will bark but still be satisfied that some of them cannot bark. This is when we will use the extensions. To make it possible for any dog ​​to bark, since dogs will often bark “woof”, we will leave them as they are. For example:

When any dog ​​breed adopts protocol Dog, it will all know how to bark “woof” the same, for example:

As the above example, we can see that, when initializing the code of the bark function inside the extension it implies that the class / struct / enum will not need to add that function inside it ( Optional protocol functions ), but It will use the function defined inside the extension. However, if the breed is dumb, here I will create a Poodle, not knowing how to bark.

Then, at this point, we can adopt the function bark again to perform an action, a separate function instead of like other dogs that barking, this dog will be mute. In addition, we could also add other functions to the protocol ( Additional functionality ) so that the classes / struct / enum that are adopting it can be used. In the Dog extensions above, I will add one more func:

at this time with both the Poodle and Golden dog above will be able to use

Below is the full code in the post

End

I would like to stop the article here, the article includes a little theory and illustrative examples, hope it is easy to understand. If you want, you can read more about the protocol and extensions in Swift ‘s docs

Share the news now

Source : Viblo