The Hunger: An introduction to protocols in Swift

March 30, 2015

“Hungry Owls”
(Swift source code)

How do these virtual owls know how hungry they are?  They rely on their hunger meters.  By defining a protocol, the owl will be able to monitor its looming starvation.  A second protocol allows the owl to pester the mother for food.

All of the communication happens between these three classes:

MotherViewController.swift
OwlViewController.swift
HungerMeterView.swift

(This would be a good time to download the source code and play with feeding the owls.)

Protocol 1: HungerMeterView updating OwlViewController with hunger amount.

From the documentation:
“A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol doesn’t actually provide an implementation for any of these requirements—it only describes what an implementation will look like. 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 our case, the HungerMeterView is going to define a blueprint of a hunger level method so that the OwlViewController can know how hungry it is.  This is often described as defining a contract that another class will sign.  The OwlViewController will conform (sign the contract) to the protocol so that it can know its hunger level.

Step one is to define the protocol in the HungerMeterView class.  The method within the protocol is the method that will be used by the OwlViewController class.

//
protocol HungerMeterProtocol {
    
    func updateHungerToOwl(hungerAmount: CGFloat)
}
//

The next step is to define the HungerMeterView delegate.

From the documentation:
“Delegation is a design pattern that enables a class or structure to hand off (or delegate) some of its responsibilities to an instance of another type.”

//
var delegate:HungerMeterProtocol?
//

The delegate call here initiates the communication with the OwlViewController class.
The hunger level is set.  If the OwlViewController has conformed to this protocol correctly, it will receive the hunger level.

//
func setHungerAmount(amt: CGFloat) {
     //set the hunger meter amount (see source code for all code)  
     delegate?.updateHungerToOwl(newHeight)
}
//

Now let’s look at the receiving end of this communication channel in the OwlViewController class.  The first thing to do is register for the HungerMeterProtocol delegate.

//
class OwlViewController: UIViewController, HungerMeterProtocol {
//

To fulfill the contract, the OwlViewController must implement the method defined in the protocol.

//
func updateHungerToOwl(hungerAmount: CGFloat) {

     //handle info from hunger meter class (see source code for all code)      
}
//

The last step is to turn the protocol on.  In the OwlViewController, set the reference with the HungerMeterView delegate.

//
     _myHungerMeter.frame = CGRect(x: 0, y: 20, width: 6, height: 70)
     _myHungerMeter.label = label
     _myHungerMeter.delegate = self
     self.view.addSubview(_myHungerMeter)
//

Protocol 2: OwlViewController updating MotherViewController with hunger plea

The second protocol is pretty simple.  You touch the OwlViewController and a message of hunger is sent to the MotherViewController.  Instead of walking through the code, let’s use Protocol 2 to make a protocol creation checklist.  Review the source code to see the actual implementation.

Creating a protocol checklist

1. Define the protocol blueprint in the class where the communication will originate.

2. Set the delegate property in the originating class.

3. Set up the method in your originating class that will send out the communication via your protocol method.

4. Register the delegate in the class where you want to receive the communication.

5. Set up the method in the receiving class that will capture the communication.

6. Set the delegate reference with your child class where you create it.

“Hungry Owls”
(Swift source code)

CREDITS
Guide by @jetstream
Illustrations by everyones_ok
threeOwls

owl_lady

Tags: ,

One Comment

  1. […] everyones_ok My friend and superior programmer Josh Kneedler (aka @jetstream) published an Introduction to Protocols in Swift via a cute little app that’s worth a […]