Skip to content

Protocol Oriented Programming in Swift

Platform Language License

"A protocol defines a blueprint of methods, properties… The protocol can then be adopted by a class, structure, or enumeration." - Apple

  1. An advantage of protocols in Swift is that objects can conform to multiple protocols.
  2. POP encourages flat and non-nested code.
  3. Swift supports only multiple inheritance of protocols.
  4. Value Type:- (Although value types do not support inheritance in Swift, they can conform to protocols.)

Defining struct:

We can create struct as person

struct Person {
    var firstName: String
    var lastName: String?
    var experience: Int
}

Defining protocol:

protocol Details {
    var fullName: String { get }
    func sayHi()
}

  1. I have created protocol as Details, to get info about person
  2. Inside the protocol block, when we describe a property, we must specify whether the property is only gettable { get } or both gettable and settable { get set }

When we fail to mention { get } or { get set } to a property, it show error like

Protocol error

Extension for struct:

extension Person: Details {
    var fullName : String {
        get {
            return firstName + " " + (lastName ?? "")
        }
    }

    func sayHi() {
        print("Hi, My self \(fullName), I have \(experience) years of experience in iOS Development.")
    }
}

Just call like

let person = Person(firstName: "Sivabalaa", lastName: "Jothibose", experience: 6)
person.sayHi()

Output:

Hi, My self Sivabalaa Jothibose, I have 6 years of experience in iOS Development.

Protocol Composition:

"Multiple protocols at the same time"

protocol Experience {
    var experience: Int { get }
}

protocol Domain {
    var domain: String { get }
}

struct Person: Details, Experience, Domain {
    var fullName: String
    var experience: Int
    var domain: String

    func sayHi() {
        print("Hi, My self \(fullName), I have \(experience) years of experience in \(domain).")
    }
}

Just call like

let person = Person(fullName: "Sivabalaa", experience: 6, domain: "iOS Development")
person.sayHi()

Output:

Hi, My self Sivabalaa Jothibose, I have 6 years of experience in iOS Development.

Optional Protocol:

@objc protocol Details1 {
    var experience: Int { get }
    @objc optional var domain: String { get }
}

Here domain will be optional, we may include or we may not.

Protocol Extension:

protocol SayHi {
 func sayHi()
}

We can create extension for protocol

extension SayHi {
    func sayHi() { 
        print("Hi, I am Sivabalaa.")
    }
}

struct Details: SayHi {}

You can just call like

let details = Details()
details.sayHi()

Protocol as Type (Last):

protocol SayHi {
 func sayHi()
}

We can create struct as StructDetails

struct StructDetails: SayHi {
    func sayHi() {
        print("Hi, This is from Struct")
    }
}

We can create class as ClassDetails

class ClassDetails: SayHi {
    func sayHi() {
        print("Hi, This is from Class")
    }
}

Now, let’s make object for struct & class

let structDetails = StructDetails()
let classDetails = ClassDetails()

Now, you can add them into an array.

let arraySayHi: [SayHi] = [structDetails, classDetails]
for sayhi in arraySayHi {
    print(sayhi.sayHi())
}

Output:

Hi, This is from Struct
Hi, This is from Class

protocol is wonderful, try it in your code & enjoy.

Reference:

Toptal

Github

Language

Let's grow together 🌱

Cheers 🍻