All about protocols in swift

Protocol oriented programming , like functional programming is an important concept in swift . I am learning protocols in swift and I am documenting it here. Everything I know about protocols will be here in this article. Do read and update your knowledge.

Image for post
Image for post

So, keeping it simple, a protocol says a struct , class or enum that if you want to be THAT, do THIS, this and THis. Example: if you want to be a human, you have to EAT, SLEEP, and Take REST and repeat!!! 🤓.

Protocol Syntax

Image for post
Image for post
protocol syntax
Image for post
Image for post
struct adopting multiple protocols
Image for post
Image for post
Classes with superclass
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { }
class ViewController: UIViewController {}extension ViewController: UITableViewDataSource, UITableViewDelegate
{
//implement protocol methods ands variables here..
}

Adding Property Requirements

Image for post
Image for post
get-set and get
Image for post
Image for post
type properties in a protocol declaration

What does conforming to a protocol means?

Image for post
Image for post
simple protocol conformation
Image for post
Image for post
protocol conformation as computed property

Method Requirements

Image for post
Image for post
Struct conforming to a protocol having methods
Image for post
Image for post
class conforming to a protocol having methods

Protocols with mutating methods

Image for post
Image for post
Struct with mutation functions

Coming back to mutating method requirements:

Image for post
Image for post
enum and class implementing a protocol with mutating function

Initializer Requirements

Image for post
Image for post
protocol with initializers

Class Implementations of Protocol Initializer Requirements

Image for post
Image for post
class conforming to protocol initializers
protocol SomeProtocol {init()}class SomeSuperClass {init() {// initializer implementation goes here}}class SomeSubClass: SomeSuperClass, SomeProtocol {// "required" from SomeProtocol conformance; "override" from SomeSuperClassrequired override init() {// initializer implementation goes here}}

Failable Initializer Requirements

Protocols as Types

Delegation

Image for post
Image for post
delegation callback

Adding Protocol Conformance with an Extension

Image for post
Image for post
Adding Protocol Conformance with an Extension

Declaring Protocol Adoption with an Extension

Image for post
Image for post
Declaring Protocol Adoption with an Extension

Collections of Protocol Types

Image for post
Image for post
Collections of Protocol Types

Protocol Inheritance

Image for post
Image for post
protocol inheritance

Class-Only Protocols

Image for post
Image for post
Class-Only Protocols
error: non-class type ‘XXXXXXXX’ cannot conform to class protocol “YYYYYY.”

Protocol Composition

Image for post
Image for post
Protocol Composition swift 4

Checking for Protocol Conformance

Optional Protocol Requirements

First, we add @objc keyword in front of the protocol keyword. Then we add the @objc optional keyword in front of our optional method/variable.

Image for post
Image for post
optional Protocol Requirements

Protocol Extensions

Image for post
Image for post
Protocol extensions

Providing Default Implementations using protocol Extensions

Image for post
Image for post
Providing Default Implementations using protocol Extensions

Adding Constraints to Protocol Extensions

Image for post
Image for post
Adding Constraints to Protocol Extensions

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store