Explaining iOS Delegates: A Story About Your Morning Coffee

One of the hardest programming concepts for me to understand in the past few weeks as I’m learning iOS development was, you guessed it, Delegates! So I’d like to share how I’ve come to think of them… using a CoffeeShop analogy. So here is the story:

You wake up this morning, and you want coffee. But alas, you can’t make coffee yourself. So you get up, get dressed and go to a coffee shop. You know that every coffee shop conforms to a specific protocol. You walk in and say “makeMeMyCoffee”, and they make you your coffee. In other words, you’re delegating coffee making to a coffee shop:

// CoffeShopDelegate.h

@protocol CoffeeShopDelegate <NSObject>

- (void)makeMeMyCoffee

@end

So when you wake up, you want to delegate out your coffee making:

// You.h
#import "CoffeeShopDelegate.h"

@interface You : NSObject

   @property (strong, nonatomic) id<CoffeeShopDelegate> coffeeDelegate;

@end

// You.m

@implementation You

- (void)whenIWakeUp
{
   // you don't want to make the coffee yourself, 
   // so you delegate it out to the delegate (the coffee shop)
   [self.coffeeDelegate makeMeMyCoffee];
}

@end

Each coffee shop, whether it’s Starbucks or Dunkin’ Donuts or your neighborhood mom and pop coffee shop, conform to the CoffeeShop protocol. When you walk in and say “makeMeMyCoffee” they know to make you coffee. Let’s say this morning you’re feeling like Starbucks:

// Starbucks.h
#import "CoffeeShopDelegate.h"
#import "You.h" // you walk into the coffeeshop

@interface StarbucksViewController : UIViewController <CoffeeShopDelegate>

@end

//Starbucks.m
@implementation StarbucksViewController

- (void)viewDidLoad
{
   [super viewDidLoad];

   // Starbucks is now your coffee delegate, in charge of making you coffee
   You.coffeeDelegate = self; 
}


- (void)makeMeMyCoffee
{
   addSlowRoastedCoffee;
   addTwoSpoonsOfSugar;
   addASplashOfSyrupyGoo;
   addHalfCupOfMilk;
   GiveCoffeeToCustomer;
}

@end

If you decided to go with Dunkin’ Donuts, they would have a different implementation of the makeMeMyCoffee method:

// DunkinDonuts.h
#import "CoffeeShopDelegate.h"
#import "You.h"

@interface DunkinDonutsViewController : UIViewController <CoffeeShopDelegate>

@end

// DunkinDonuts.m
@implementation DunkinDonutsViewController

- (void)viewDidLoad
{
   [super viewDidLoad];
   
   // Dunkin Donuts is now in charge of making your coffee
   You.coffeeDelegate = self; 
}

- (void)makeMeMyCoffee
{
   // The Dunkin Donuts coffee follows a different recipe than Starbucks Coffee
   addCoffee;
   addFiveSpoonsOfSugar;
   addASplashOfHersheySyrup;
   addAThirdCupOfMilk;
   GiveCoffeeToCustomer;
}

@end

So that is all. Delegates are basically a way to outsource your tasks, and allow different classes (Starbucks vs Dunkin Donuts) to implement the task in their own different way.

Enjoy the article? Join over 14,500+ Swift developers and enthusiasts who get my weekly updates.

  • Maciej Kołek

    This is very well explained and easy to understand. Thanks to you a lot of young iOS devs will understand how delegates work much better now. Worth sharing 🙂

  • Christoph

    No I understand Delegation – thanks a lot 🙂

  • All Time Hero

    Thank you.

  • Matías Schmid

    Really cool explanation, thank you a lot!

  • Chucky

    Thank you you made it really easy to understand for me

  • Thank you for posting this useful story. 🙂