Delegation Pattern in Swift

Williams Perdana
Blibli.com Tech Blog
4 min readDec 18, 2020

Imagine you’re a CEO of a big-sized global company with thousands of employees divided into such departments as Technology, Marketing, and Finance. With only two hands, two eyes, and two feet, you cannot control and monitor everything your employees do, that’s why you hire people to be your CTO, CMO, and CFO. They have the same vision as yours to run your company and drive them to fulfill the vision and mission of the company, with their way to achieve one goal: Making the Company run well.

Source: https://www.searchenginewatch.com/wp-content/uploads/2018/10/ceo-coo-cfo-cmo-cio-370x229.jpg

Same in the programming world, you have to divide your code blocks into different classes and functions to make your code readable, maintainable, and reusable — not a big file to run it all. This approach is also called as Object-Oriented Programming. OOP is often referred to as 3 big paradigm: Encapsulation, Inheritance, and Polymorphism. In Swift, we have this polymorphism-like style, or Interface (if you code in Java), which is called as Protocol.

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

https://docs.swift.org/swift-book/LanguageGuide/Protocols.html

A protocol contains a set of functions that defines some capabilities of which classes that conformed it. For example:

All the mammals conformed to that protocol has different ways to give birth, to eat, and to sleep. They have to define how they do those functions on their own class file. You can also give the default implementation like this:

Tired of all these fiction things? Let’s have a real-application example:

Root View Controller

We want to create a simple application that a list of books with 2 different genres: Fictional and Educational. And if we click on the button, it will push one view controller that shows the list of the books we entered (using UITableView), with one button on the navigation bar.

Book List View Controller

When the plus button on the navigation bar clicked, it will show an input view controller to accept book name, then add it to the book list.

InputTitleVC

After the save button clicked, it will dismiss the modal and then call a function named updateData on FictionalBooksTableVC to reload the tableView on it. Then, how if we want to use this InputTitleVC to be used on EducationalBooksTableVC, but with a different implementation of saving books? Should we add a new variable named previousController2: EducationalBooksTableVC? then call the desired function to accommodate this? How if 10 view controllers want to reuse the same VC ?

That’s why this delegation pattern can be used on this problem. To start, we only declare a protocol that reflects the saving methods. For example:

Then on your FictionalBooksTableVC:

On line 26, you see that inputController.delegate = self. It means that the delegate property on InputControllerVC has the implementation declared on FictionalBooksTableVC. Then, you will face one error message:

Cannot assign value of type ‘FictionalBooksTableVC’ to type ‘InputTitleVCDelegate’

It is because you haven’t implemented any of InputTitleVCDelegate on your own class, which is mandatory if you don’t have default implementation on your protocol. You can simply conform InputTitleVCDelegate on your class, and write the implementation on your class.

Tadaaa…! You have your own saving implementation declared on the desired class, not on the InputTitleVC that can be used on another VC. This is how the delegation pattern implemented on your application.

--

--