DEV Community

Ajithmadhan
Ajithmadhan

Posted on

swift - protocols

Protocols provide a blueprint for Methods, properties and other requirements functionality. It is just described as a methods or properties skeleton instead of implementation. Methods and properties implementation can further be done by defining classes, functions and enumerations. Conformance of a protocol is defined as the methods or properties satisfying the requirements of the protocol.

Syntax
Protocols also follow the similar syntax as that of classes, structures, and enumerations −

protocol SomeProtocol {
   // protocol definition 
}
Enter fullscreen mode Exit fullscreen mode

Protocols are declared after the class, structure or enumeration type names. Single and Multiple protocol declarations are also possible. If multiple protocols are defined they have to be separated by commas.

struct SomeStructure: Protocol1, Protocol2 {
   // structure definition 
}
Enter fullscreen mode Exit fullscreen mode

When a protocol has to be defined for super class, the protocol name should follow the super class name with a comma.

class SomeClass: SomeSuperclass, Protocol1, Protocol2 {
   // class definition 
}
Enter fullscreen mode Exit fullscreen mode

Notes:
The protocol just holds the method or properties definition, not their actual body.
The protocol must specify whether the property will be gettable or gettable and settable.

Conform Class To Swift Protocol

In Swift, to use a protocol, other classes must conform to it. After we conform a class to the protocol, we must provide an actual implementation of the method.

Here's how to conform a class to the protocol,

protocol Greet {

  // blueprint of a property 
  var name: String { get }


  // blueprint of a method 
  func message() 
}   

// conform class to Greet protocol
class Employee: Greet {

  // implementation of property
  var name = "Perry"

  // implementation of method
  func message() {
    print("Good Morning!")
  }
}
Enter fullscreen mode Exit fullscreen mode

Conforming Multiple Protocols

In Swift, a class can also conform to multiple protocols. For example,

protocol Sum {
  ...
}

protocol Multiplication {
  ...
}

class Calculate: Sum, Multiplication {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Swift Protocol Inheritance

Similar to classes, protocols can inherit other protocols. For example,

protocol Car {
  ...
}

protocol Brand: Car {
  ...
}
Enter fullscreen mode Exit fullscreen mode

Here, the Brand protocol inherits the Car protocol. Now, if any class implements Brand, it should provide implementations for all properties of both Car and Brand.

Example,

protocol Car {
  var colorOptions: Int { get }
}

// inherit Car protocol
protocol Brand: Car {
  var name: String { get }
}

class Mercedes: Brand {

  // must implement properties of both protocols 
  var name: String = ""
  var colorOptions: Int = 0
}

var car1 = Mercedes()
car1.name = "Mercedes AMG"
car1.colorOptions = 4

print("Name:", car1.name)
print("Color Options:", car1.colorOptions)
Enter fullscreen mode Exit fullscreen mode

Protocol Extensions

In Swift, we can extend protocols using the extension keyword. For example,

// protocol definition
protocol Brake {
  func applyBrake()
}

// define class that conforms Brake
class Car: Brake {
  var speed: Int = 0

  func applyBrake() {
    print("Brake Applied")
  }
}

// extend protocol
extension Brake {
  func stop() {
    print("Engine Stopped")
  }
}

let car1 = Car()
car1.speed = 61
print("Speed:", car1.speed)

car1.applyBrake()

// access extended protocol
car1.stop()

Enter fullscreen mode Exit fullscreen mode

Top comments (0)