Protocols in Swift

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
May 14, 2023

Working with Protocols in Swift

Protocols in Swift are used to specify the blueprint of methods, properties, and other requirements that suit a particular task or functionality. A protocol is a collection of methods, properties, and other requirements that can be adopted by a class, struct or enum. It describes what should be done rather than how to do it. In this tutorial, we will learn how to define and implement protocols in Swift with the help of code snippets.

Defining a Protocol

To define a protocol in Swift, use the “protocol” keyword followed by the protocol’s name, as shown below.
protocol MyProtocol {
  // protocol requirements go here
}
Using a protocol to define a blueprint for a class, struct or enum involves specifying the methods, properties, and other requirements that a conforming type should implement. Here is an example to understand it better:
protocol Vehicle {
  var numberOfWheels: Int { get }
  var color: String { get set }
  
  func start()
  func stop()
}
In the code above, we have defined a protocol named “Vehicle”. This protocol has two properties named “numberOfWheels” and “color”. Both of these properties are required to be implemented by the conforming type. The “numberOfWheels” property is read-only, whereas the “color” property is read-write. The “Vehicle” protocol also has two methods named “start” and “stop”. Any conforming type that implements this protocol must also implement these two methods.

Implementing a Protocol

To implement a protocol, use the “class”, “struct”, or “enum” keyword followed by the name of the conforming type and then the protocol’s name, separated by a colon, as shown below.
struct Car: Vehicle {
  var numberOfWheels: Int
  var color: String
  
  func start() {
    // start the car
  }
  
  func stop() {
    // stop the car
  }
}
In the above example, we have defined a struct named “Car” that conforms to the “Vehicle” protocol. We have implemented both the required properties and methods of the “Vehicle” protocol. Now any instance of “Car” can be treated as a “Vehicle” object.

Optional Protocol Requirements

Sometimes it’s not necessary for the conforming types to implement all the requirements of a protocol. In such a scenario, we can make the requirements optional by marking them as “optional”. Here is an example:
@objc protocol MyProtocol {
  @objc optional func doSomething()
}
In the code above, we have marked the “doSomething” method as optional using the “optional” keyword. In addition to that, we have used the “@objc” attribute before the protocol declaration. The reason we are doing this is that optional protocol requirements are only available for Objective-C APIs.

Conclusion

Protocols in Swift are used to define a blueprint of methods, properties, and other requirements that suit a particular task or functionality. In this tutorial, we have learned how to define a protocol, implement a protocol and how to make the requirements of a protocol optional.