Skip to content
DeveloperMemos

Implementing a Protocol in Swift

Swift, Protocol, Programming1 min read

Protocols are a fundamental concept in Swift and object-oriented programming. They are used to define a set of methods, properties, and other requirements that a class or structure must implement. Protocols provide a way to standardize the interface of types, making them more flexible and easier to use.

Defining a Protocol

Before implementing a protocol, we first need to define it. To define a protocol in Swift, we use the protocol keyword followed by the name of the protocol:

1protocol MyProtocol {
2 // Protocol requirements go here
3}

Inside the protocol, we can define requirements, such as properties and methods, that conforming types must implement. For example:

1protocol Animal {
2 var name: String { get set }
3 func makeSound() -> String
4}

In this example, we define a protocol Animal with two requirements: a name property and a makeSound method. The name property is defined as a read-write property, and the makeSound method returns a String.

Implementing a Protocol

Once we have defined a protocol, we can implement it in a class, struct, or enum by conforming to the protocol. To conform to a protocol, we simply list the protocol name after the type name, separated by a colon:

1struct Cat: Animal {
2 var name: String
3 func makeSound() -> String {
4 return "Meow"
5 }
6}

In this example, we define a Cat struct that conforms to the Animal protocol. We implement the name property as a variable and the makeSound method to return "Meow".

We can also conform to multiple protocols by separating them with commas:

1class Dog: Animal, Equatable {
2 static func == (lhs: Dog, rhs: Dog) -> Bool {
3 return lhs.name == rhs.name
4 }
5
6 var name: String
7 func makeSound() -> String {
8 return "Woof"
9 }
10}

In this example, we define a Dog class that conforms to both the Animal protocol and the Equatable protocol. We implement the name property as a variable and the makeSound method to return "Woof". We also implement the == operator required by the Equatable protocol to compare two Dog instances by their names.

Using a Protocol

Once we have implemented a protocol, we can use it just like any other type. For example, we can create an array of Animal instances and call their makeSound methods:

1let cat = Cat(name: "Whiskers")
2let dog = Dog(name: "Fido")
3let animals: [Animal] = [cat, dog]
4
5for animal in animals {
6 print("\(animal.name) says \(animal.makeSound())")
7}

This code creates a Cat instance named cat, a Dog instance named dog, and an array of Animal instances named animals that contains both. We then loop through the array and call the name and makeSound() methods for each animal instance, which prints out their name and the sound they make.

Conclusion

In this article, we have discussed how to define and implement a protocol in Swift with some simple examples, as well as how to conform to multiple protocols and use them in practice. Protocols are a powerful tool in Swift that allow us to define a standardized interface for types and make our code more modular and flexible - I hope this article has helped you understand them a little better!