Delegation pattern in Swift 5.1

What is the Delegate Protocol in Swift? | optional and required Protocol methods?

Omer Rahmanovic
4 min readDec 23, 2020
Photo by Danial RiCaRoS on Unsplash

What is the 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.

In addition to specifying requirements that conforming types must implement, you can extend a protocol to implement some of these requirements or to implement additional functionality that conforming types can take advantage of.

The protocol you have seen so many times while development of iOS apps most commonly in UITableViewDelegate & UITableViewDataSource. Both of these protocols have their optional and required methods.

In this article we will go through some basics about delegation protocol in Swift and show you how to implement both optional and required protocol methods.

Why we use Delegation?

A few reasons in favour of delegation:

1. Delegation, and the delegation pattern is a lightweight approach to hand-off tasks and interactions from one class to another.
2. You only need a protocol to communicate requirements between classes. This greatly reduces coupling between classes.
3. It separates responsibilities of the class that generates interactions from the class that responds to these interactions.

Delegation vs. Subclassing

An compelling alternative to delegation is subclassing. Instead of using a delegate to get GPS location updates from CLLocationManager, you simply subclass that manager class and respond to location updates directly.

This has a massive disadvantage: you inherit the entire CLLocationManager class, for something as simple as getting a bit of location data. You would need to override some of its default functionality, which you either have to call directly with super or replace entirely.

And lastly, subclassing creates a tightly-coupled class hierarchy, which doesn’t make sense unless your subclass is similar in nature to the class you’re subclassing. This is not likely if you’re merely responding to GPS location updates.

Delegation vs. Notification Center

What about the Observer pattern, as found in NotificationCenter, as an alternative to delegation? It could work: you’d respond to observable changes in a Location object, for instance.

The Observable pattern is useful when your code needs to communicate with multiple components, with a one-to-many or many-to-many relationship. One component in your app broadcasts a signal that multiple other components respond to. And apart from a broadcast type and some associated data, you can’t formalize the requirements for the communication like a protocol can.

Now, after we explained what are protocols and difference between some other options in Swift let’s get to the practical part of this article.

First thing we do is to create delegate inside of our View controller.

After we created our protocol next thing is to make instance of it inside DelegationModalVC

For this article i made my UI completely programmatically so we need to create button and action for it inside of our View Controller. In our button we are just going to take data from UITextField and provide it to the delegate method. After that we will dismiss our View Controller.

Now we can start working on View Controller where all changes will be applied using our delegate method. First thing we will do is to make extension and inherit our Delegate.

Also we need to add button with action to open our View Controller where we defined Delegate with its method.

After user enter some text into UITextField on DelegationModalVC and press Done button these changes will be applied to our base view.
For this tutorial that would be all. It is pretty simple and easy one to do and very useful even tho it is kinda old approach.

If you missed something or don’t know how to do certain things on you own here you can find source code:

--

--