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.Screen Shot at . . AM 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:

Screen Shot at . . PM


  •  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.


  •  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”

Screen Shot at . . AM

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.

Screen Shot at . . AM

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


  • 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:-

Screen Shot at . . PM

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

Screen Shot at . . PM

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.

Screen Shot at . . PM

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


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

Screen Shot at . . PM

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.

Screen Shot at . . PM


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


  • 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.


Screen Shot at . . PM

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

Screen Shot at . . PM

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.

2 thoughts on “An Introduction To Protocol Oriented Programming in Swift (Part-2)”

  1. sir,this content is very useful for us.
    please suggest us by what can we use multiple programming languages in one app development….

  2. People can easily all reap the benefits of studying more about themselves and our overall
    health and well being. Specified activities and activity
    amounts can possess great advantage to all of us, and all of us ought to find out
    even more about them. Your blog seems to have presented
    vital data which is to be useful to numerous populations and
    individuals, and I appreciate your writing your skills in this way.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.