3, సెప్టెంబర్ 2018, సోమవారం

An Introduction To Protocol Oriented Programming in Swift (Part-2)

In the previous article, we came across what is Protocol oriented programming and why Apple suggest their developers to use it and also saw the basics for why value types are preferred over reference semantics in Swift programming. If you did not read that article, I will suggest reading that first to get more out of this one.

Also Read: An Introduction To Protocol Oriented Programming In Swift

In this article, I’ll try to make you understand how to use Protocol in Swift and Try to clarify how you can write protocols and protocols extensions. So, Let’s dive deep into the topic.

1. Writing Your First Protocols

One of the limitations that we had in the past was the unavailability of multiple inheritances in Swift. With classes in Swift, we just had single inheritance. When we talk about multiple inheritances, one class can have more than one superclass. This problem can be solved by using Protocol oriented programming. Protocols are like the kind of contracts or the blueprints that define the functionality for what a class or structure may possess. In protocols, we don’t define how that functionality will be achieved but, we just define the requirements for types that conform to that protocol. Some other languages also feature the similar behavior that we call as Interfaces.

We also have protocol extensions through which one can extend the defined protocol and can actually provide the more common functionality at any place in the project. Something very cool right!

Things to keep in mind while making protocols are that:

  • you can’t associate any values directly with the defined properties and requirements inside a protocol.
  • The defined properties can be a function or computer properties and they must be computed later on in the program.

Coding The First protocol( Defining A protocol):

Protocol Syntax:

Note:-

  •  A {get set} is used to define whether the defined property is read-only or read-write property. Here it is both read and write property.
  • A {get set} property cannot be a constantly stored property. It should be a computed property and both “get” and “set” should be implemented.
  • A ”get” property can be any kind of property, and it is valid for the property to be also settable if required.

Functions defined in the protocols are the blueprints and whatever conforms to the “Car protocol” has to implement this “started function” and “off function” in their body or code.

Note:-

  •  If you use the above function with value types then you need to define the above function as follows:

        mutating func started()

        mutating func off()

  • {Mutating} keyword is used here for value types such as with structure or enums, how we will get to know that a structure will use this? well, if you have any function or code that can modify the internal properties then this “mutating” keyword is required.

  •  The “mutating” keyword is only used by structures and enumerations not used with classes.

 Confirming to a protocol:

A protocol defines the behavior and tells that you need to do few things in order to become something i.e. Example: If you want to drive a swift car then, you should conform to protocol “MyCar”

As you declare a structure names “Swift” it will give you an error shown above. Why is this error Prompting on Xcode?. The answer is very simple i.e. Xcode is suggesting us that we have not included all the behavior of the protocol “MyCar” with our value type Swift structure. Once you click on the fix button then you will the following.

The structure Swift is bound to implement the “started ” and “off” methods.

Note:-

  • A protocol can have type methods or instance methods i.e structure or enums are type methods and classes are instance methods.

  • Methods are declared in exactly the same way as for normal instance and type methods but without curly braces or a method body.

  • Variadic parameters are allowed.

  • Default values are not allowed.

So, the whole code with our mutating function with structure “swift” looks like this as shown below:-

Protocols can also refer to the reference type as said earlier as shown below:-

Apart from the functions that have been defined in the protocol, you can also make more functions inside the class or your structure.

Array with protocols:

we can also define an array that can contain the values of different types when it conforms to a protocol and this the power of protocol that comes into play as shown below.

You can notice here the array  myarray has multiple type value one is of class and other is of the structure.

Note:-

  • You can’t use the custom methods that you have defined in your class or structure with the above functionality. For Eg:-

you get an error in the print statement saying that the “.abs” is not the member of the type MyCar. For using the abs method you can use the way shown below.

 

Use {if let} statement and cast the array as the truck instead of MyCar then the abs method will work for you.

Note:-

  • A protocol can also conform to something that may be a superclass for multiple inheritances. In that case, whatever conforms to your defined protocol must implement the public methods of that superclass and this is what exactly we do in our delegates methods implementations.

Protocols Extensions:

The real power comes to a protocol with its extension property. This is where all the magic happens of the POP concept. We can extend any protocol to give them more functionality as per our instant requirements. You can extend any pre-defined protocol or your custom protocol.

Syntax:

Here after extension keyword, you can give your protocol name. We implement the extension as shown below.

We can implement the functionality inside the extension and what so ever conform to this protocol can inherit that functionality. In the above code, the {class Swift} does have the {“started and off methods”} but as it conforms to the “MyCar protocol” actually automatically inherit the functionality as the methods are defined in the extension of the MyCar protocol.

Well, that’s it for today. Now you know what a protocol is and where to use them and also a bit about extending them. If you enjoyed reading this post, please share so others can find it.

0 కామెంట్‌లు:

కామెంట్‌ను పోస్ట్ చేయండి