Layman’s iOS Delegate

A high-level, understandable definition to the most baffling concept in iOS programming.

Starts with what we all understand about a delegate in the real world.

You are working in a company. You’re rushing to meet a deadline, and someone is assigning you to attend a meeting which you can’t care less to attend in the situation.

You don’t want to decline, since that’s probably upset some people especially your boss (in fact, it might have been him who have delegated this meeting to you). You have no choice but to delegate.

delegate (n.): a person authorized to act as a representative for another; a deputy

It looks like Tom, your direct subordinate, has just finished his work and is whistling to his tune on Spotify. Clearly, he’s idling and could use some meetings. You walk to him and make him your delegate to a meeting.

Tom, annoyed: “What, huh? What meeting?”
You: “Yes, this meeting is in the next ten minutes.”
Tom: “Oh, well. You’re my boss. Brief me then.”
You: “You prepare yourself and go sit in there. I’ll have someone send you the information later.”
Five minutes after Tom entered the meeting room, you have your assistant send him a piece of paper with a list of information just enough for him to perform instead of you with a document attached under “Material.”

Now, let’s reinterpret the situation in Swift code.

To sign up Tom as your delegate, Tom is supposed to conform to a protocol. You can think of it as a badge which you sign off that Tom can bring into the meeting to show attendees he’s there for you.

protocol MeetingDelegate {
func attendMeeting(who attendees: [String],
when time: NSTimeInterval,
what topic: String,
callback handler:
([String], [String]) -> [String]))
func handOutListOfIdlers(to: [String]) -> [String]
}

Now, Tom just has to adopt this protocol, or pick up the badge:

class Tom: IdleEmployee, MeetingDelegate {
// implement the protocol's methods
func attendMeeting(who attendees: [String],
when time: NSTimeInterval,
what topic: String,
callback handler:
([String], [String]) -> [String]))
{ // intruction for Tom }
func handOutListOfIdlers(to: [String]) -> [String]
{ // instruction for Tom }
}

“What about all the data supplies?” You thought. Well, one thing about delegates is they need not know anything (or at least very little) about what they are supposed to be doing for you. In this case, you (or an instance of You class) knows when to call Tom’s methods and supply him with relevant arguments by sending that piece of paper and attached material to him. Tom just have to ready himself to receive the message.

Ideally, anyone could have conformed to a protocol and act as a delegate for some other identity. However, in the MVC paradigm, controller instances are ideal delegates. Normally, contrasting to real-world scenerio, you assign the controller that contains the sub controller as a delegate to the latter.

I’m also picking up a lot about Swift and iOS development, and I hope this memoir serves as a clear, conceptual anecdote to delegates.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.