Creating a Delegate in Swift

The Delegate pattern is one that's widely used is developing apps for iOS and OS X and it's one that's very simple to implement in Swift. It's primarily used to alert classes of changes or interactions in other instances.

For example, UITableView's delegate has a variety of methods of which include notifying when a cell has been selected or edited.

Apple describes a delegate as the following in their documentation:

A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.

Delegate == Protocol

Creating a delegate is as simple as creating a protocol — that's pretty all it is. Here's a simplified delegate from Fetch.

protocol CastHandlerDelegate: class {

    /// Media Launched successfully on the cast device
    func launchObjectSuccess()

}

Putting this delegate to use is a two-step process. First, we have to add the delegate to the class we want to call it from — in this case CastHandler. Then we implement the delegate on the class we want to receive notifications on and make it compliant.

Calling the delegate

To use the delegate we add an optional variable to the class we'll be sending notifications from.

weak var delegate: CastHandlerDelegate?

We can then call this from anywhere within that class like so:

delegate?.launchObjectSuccess()

Thanks to Swift's optionals, if the delegate hasn't been set it won't call the method and we don't have to worry about anything else. If it has been set then our launchObjectSuccess method will be executed.

Conforming to CastHandlerDelegate

As you probably already know, the next step is to conform to the delegate. First, we tell the class it needs to implement the methods set in the delegate.

class MyViewController: UIViewController, CastHandlerDelegate {

}

Next, we implement the methods within our delegate.

class MyViewController: UIViewController, CastHandlerDelegate {
    
    func launchObjectSuccess() {
        println("Media launched!")
    }

}

That's it! Our delegate has been created and we're successfully calling the method.

Optional Methods

Protocol's in Swift are essential interfaces and tell a class they must implement the methods and properties outlined within them. This is different to Objective-C that had optional and required methods and properties.

If you try to use the optional keyword within a Swift protocol you'll get the following error.

Optional Error

Should you have a reason to implement optional methods you can override Swift's rulebook and use Objective-C style style protocols by adding the @objc tag to your protocol.

@objc protocol CastHandlerDelegate { }

As if by magic, the nasty red error message will disappear and you can now use your optional method as follows. Take note of the additional ?.

delegate?.anOptionalMethod?()
Work With Us! We're currently accepting new projects at Cocoon. If you have a cool new thing you want us to help with, feel free to drop me a line ✌️