Close Menu
Technotification
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram
    Technotification
    • Home
    • News
    • How To
    • Explained
    • Facts
    • Lists
    • Programming
    • Security
    • Gaming
    Technotification
    Home › Programming › An Introduction To Protocol Oriented Programming in Swift (Part-2)

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

    By Harshit SatyaseelDecember 3, 2022
    Facebook Twitter Reddit LinkedIn
    Protocol Oriented Programming in Swift

    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.

    Contents

    • 1. Writing Your First Protocols
    • Coding The First protocol( Defining A protocol)
        • Protocol Syntax
        •  Confirming to a protocol
        • Protocols can also refer to the reference type as said earlier as shown below-
        • Array with protocols
    • Protocols Extensions

    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

    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”

    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.

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

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

    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.

    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:

    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.

    Share. Facebook Twitter LinkedIn Tumblr Reddit Telegram WhatsApp
    Harshit Satyaseel
    • Website
    • LinkedIn

    Research Intern, INSA Rouen | Technical Writer, Self-learner and Tech exuberant person.

    Related Posts

    The Best Python Libraries for Data Visualization in 2025

    April 1, 2025

    Is C++ Still Relevant in 2025 and Beyond?

    February 20, 2025

    5 Best Programming Languages for Machine Learning in 2025

    February 18, 2025

    10 Must-Have Chrome Extensions for Web Developers in 2025

    February 17, 2025

    Difference Between C, C++, C#, and Objective-C Programming

    February 16, 2025

    How to Learn Programming Faster and Smarter in 2025

    February 14, 2025
    Lists You May Like

    10 Best RARBG Alternative Sites in April 2025 [Working Links]

    April 1, 2025

    10 Sites to Watch Free Korean Drama [2025 Edition]

    January 2, 2025

    The Pirate Bay Proxy List in 2025 [Updated List]

    January 2, 2025

    10 Best Torrent Search Engine Sites (2025 Edition)

    February 12, 2025

    10 Best GTA V Roleplay Servers in 2025 (Updated List)

    January 6, 2025

    5 Best Torrent Sites for Software in 2025

    January 2, 2025

    1337x Alternatives, Proxies, and Mirror Sites in 2025

    January 2, 2025

    10 Best Torrent Sites for eBooks in 2025 [Working]

    January 2, 2025

    10 Best Anime Torrent Sites in 2025 [Working Sites]

    January 6, 2025

    Top Free Photo Editing Software For PC in 2025

    January 2, 2025
    Pages
    • About
    • Contact
    • Privacy
    • Careers
    Privacy

    Information such as the type of browser being used, its operating system, and your IP address is gathered in order to enhance your online experience.

    © 2013 - 2025 Technotification | All rights reserved.

    Type above and press Enter to search. Press Esc to cancel.