Swift delegate design sample – The.Swift.Dev.

0/5 No votes

Report this app



Implementing delegation in Swift

You will want a delegate protocol, a delegator who truly delegates out the duties and a delegate object that implements the delegate protocol and does the precise work that was requested by the “boss”. Let’s translate this into human.

The consumer reviews a bug. The venture supervisor creates a difficulty and tells one of many builders to repair the issue asap.

See? That is delegation. Sooner or later an occasion occurred, so the delegator (supervisor) utilized an exterior useful resource (a developer) utilizing a typical interface (concern describing the issue for each occasion) to do obtain one thing (repair the ๐Ÿ›).

To show how delegation works in actual life I made a reasonably easy instance. I’ll use an analogous strategy (as a result of Xcode playgrounds are nonetheless freezing each 1-5 minutes) like I did for the command sample, however the goal of this one goes to be virtually completely completely different, as a result of we’re speaking about delegation. ๐Ÿ˜…

#!/usr/bin/env swift

import Basis

protocol InputDelegate {

    var shouldContinueListening: Bool { get }

    func didStartListening()
    func didReceive(enter: String)

class InputHandler {

    var delegate: InputDelegate?

    func hear() {

        repeat {
            guard let enter = readLine() else {
            self.delegate?.didReceive(enter: enter)
        whereas self.delegate?.shouldContinueListening ?? false

struct InputReceiver: InputDelegate {

    var shouldContinueListening: Bool {
        return true

    func didStartListening() {
        print("๐Ÿ‘ป Please be good and say "hello", if you wish to go away simply inform me "bye":")

    func didReceive(enter: String) {
        change enter {
        case "hello":
            print("๐ŸŒŽ Hiya world!")
        case "bye":
            print("๐Ÿ‘‹ Bye!")
            print("๐Ÿ” Command not discovered! Please attempt once more:")

let inputHandler = InputHandler()
let inputReceiver = InputReceiver()
inputHandler.delegate = inputReceiver

That is how one can create your personal delegate sample in Swift. You possibly can think about that Apple is doing the identical factor below the hood, with UICollectionViewDataSource, UICollectionViewDelegate and so forth. You solely should implement the delegate, they will present the protocol and the delegator. ๐Ÿค”

Weak properties, delegates and courses

Reminiscence administration is a vital factor so it is value to say that every one the category delegates ought to be weak properies, otherwise you’ll create a extremely dangerous reatin cycle. ๐Ÿ˜ฑ

protocol InputDelegate: class {  }

class InputHandler {

    weak var delegate: InputDelegate?


class InputReceiver: InputDelegate {

Right here is the altered Swift code snippet, however now utilizing a category because the delegate. You simply have to vary your protocol a bit of bit and the property contained in the delegator. At all times use weak delegate variables if you will assign a category as a delegate. โš ๏ธ

As you possibly can see delegation is fairly simple, however it may be harmful. It helps decoupling by offering a typical interface that can be utilized by anybody who implements the delegate (typically information supply) protocol. There are actually wonderful articles about delegates, if you would like to know extra about this sample, it is best to test them out.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.