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

    An Introduction to Protocol Oriented Programming in Swift

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

    Swift programming language was launched in 2014 by Apple.Inc in their WWDC yearly conference and since then it has become the most popular language for the Apple developer community. Today, I will introduce you to a new programming paradigm called Protocol Oriented Programming in swift, which was basically introduced to us in WWDC 2015 as an upgrade in the form of Swift2.o, one of its own kind in the segment.

    Protocol Oriented approach was introduced to resolve some issues in programming and it also differs in various scenarios when compared to Object-Oriented programming. So let’s dive into the topic.

    Contents

    • What is Protocol Oriented Programming?
    • Why We Need POP?
    • Conclusion

    What is Protocol Oriented Programming?

    Protocols basically serve as a blueprint like classes rather than a fully functional types for methods, properties, and other requirements. This is done to perform a piece of functionality and behaves like an interface and tells what to implement. It is a powerful feature of Swift language. Apple tells us:

                                                      “Don’t Start with the Class, Start with the Protocol”

    Why We Need POP?

    When we design any software system, then we figure out the general requirements that satisfy the given system. We, even model the relationship b/w the UI elements such as buttons, view, images etc. for which we generally start with superclass and model the relationships using inheritance.

    In Swift, as we have the concept of POP so, we can even start with protocols and can model the relationships as protocol implementations. In the protocol oriented approach, we model our system with a protocol, rely on the concepts like:

    • Protocol Extensions
    • Protocol Inheritance
    • Protocol Composition 

    In Swift, we use structs, enums, and tuples rather than working only with classes since, Value Semantics are preferred over Reference Types.

    Also, there could be various cases where OOP is not the best solution to implement. Let’s check and figure out the drawbacks in Object-Oriented concept. We know that Inheritance is one of the most important concepts in OOP but, inheritance doesn’t work for value types and modern languages like Swift prohibits to support the feature of multiple inheritances due to complexities and value types is the first citizen of Swift. So, POP does a great job by providing the ability to add multiple abilities to the class, struct, and enum with protocols and extensions that supports multiple implementations while we code.Screen Shot at . . AM Other benefits with Protocol implementations are:

    • Swift checks whether the requirements of the protocol are full-filled or not for the classes we are implementing at compile-time, so, this helps us to find out if there were any issues or bugs in code even before we ran our program.
    • Also, protocols bring more abstraction mechanism than classes do in Swift.
    • We’re not restricted to only use classes since any type, including value types, can implement a protocol. 
    • A type can implement multiple protocols.
    • We can create as many protocols as we need.

    Before I talk about POP in detail and how to use and implement it, we must understand the basics right, so let us focus on Swift Types first. Here is the diagrammatic representation of Swift Types that we have.Screen Shot at . . PM

    Well, here I am interested in the behavior of Classes and Structure, one belongs to the reference type and other to value type and observe how objects behave through an example.

    Screen Shot at . . PM

    And, this is really important to understand and why is it so? If you have a very complex hierarchy and you set something in one part of the code, it should not have any wrong consequences in the other part. So, the lesson is:

    • Classes use reference i.e if you set something to other it is not a copy instead it is a reference.
    • Whereas, in value type such as structures, passes things as a copy, not as a reference.

    Conclusion

    Apple, always recommends going with the value type while we program in Swift. Structures are preferable when we have a small copy of data and it is much safer than having multiple references to the same objects in our code. The concept becomes more important when we talk about variable and passing their value. In Swift, we work in a multi-threaded environment and if we have copies of variable then, we don’t need to worry about them in the other place where value may get change. So, using structure is advised by Apple and Protocol-Oriented Programming serves better abstraction with structures.

    Using Protocols and it’s extensions features, we can really ignore/avoid for making a huge superclass and then inheriting functionality from there. So, Hope it is now clear to you what is POP and why Apple use this.

    Note: This does not mean that we don’t use Class, there are some situations where classes are the only option left for our implementation.

    In the other part of this article, we will see how to implement and use protocols, protocol extensions, Generics with the protocol.

    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.