It’s Delegate Season!

This being an election year, ears have been buzzing with the word “delegates.” In politics, a delegate is an elected official that represents their constituents in the House. In programming, delegation is a way to connect objects and help them communicate with one another. In both fields, delegation facilitates communication between two parties. Apple defines the role of a delegate in the following fashion: “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.”I’ve had the concept of delegation explained to me through various different analogies, all very helpful, so allow me to share with you my interpretation of delegation in layman’s terms. Ladies and gentleman, may I present to you…

Storytime: The Single Girl and the Wine Glass

Once upon a time there was a Single Girl object who lived happily in her 250 sq. ft. Manhattan apartment. Single Girl was a pragmatist; her actions were ruled completely by her Brain object. Brain managed all of the important tasks for Single Girl like eating, sleeping, drinking, and walking her Chihuahua, Tito. On Friday nights, Single Girl has a Wine Glass attached to her, but Wine Glass has no way of knowing what Brain is thinking and Brain has no way of knowing what Wine Glass is thinking.

Screen Shot 2016-03-05 at 8.59.21 PM

Despite their inability to communicate, Brain is able to use Wine Glass attributes to satisfy its needs: Brain can use the Wine Glass sip function to lower Wine Glass liquid variable, but because Brain is busy enjoying The Bachelor, Brain has no way of knowing when liquid reached 0 – how will Brain ever be able to tell Single Girl to instantiate another Wine Glass?! To alleviate this issue, we use a Wine Glass Delegate. This Wine Glass Delegate allows Brain to listen out for when Wine Glass has no more liquid. All we need to do is tell Brain to listen up for Wine Glass telling its Wine Glass Delegate that liquid is at 0 so Brain can react to Wine Glass’ cries. With Wine Glass Delegate now in play, Single Girl never has to live without liquid in Wine Glass. And they lived happily ever after!

Back To Reality…

If there is one think you take from the story above, let it be that delegation, in its simplest form, is just a callback mechanism which allows a program to start something up then go perform other tasks. If there is a response generated by what was started up, a message will generate the execution of a method that will deal with the response. I found this simplified example extremely helpful in understanding the relationship between two objects: if we have an object, object A and object calls on object B to perform an action, we need a means of letting object A know that object B has completed the action so that A can perform the necessary response. Thus,

  • A is a delegate object of B
  • B will have a reference of A
  • A will implement the delegate methods of B
  • B will notify A through the delegate methods

And that’s all folks!

Get To Know Your Delegate!

The concept that was most difficult for me to grasp was who exactly could be a delegate. What types can be delegates? Can a subclass be a delegate of a superclass? What does the word “delegate” even mean?! I was basically struggling with both a programming language and the English language. So to prevent you from having to struggle through the same word-related anxiety I went through, I’ve compiled my findings below:

Who Can Be A Delegate?

Any class can have a delegate and objects of any type can be delegates. A delegate is simply a property in a class that holds a reference to an object of unknown type. If we look at the header file for UITableView, we can see this:

Screen Shot 2016-03-04 at 4.03.04 PM.png

So if we are in some custom View Controller with a Table View property, setting self.someTableView.delegate = self simply sets a pointer to point from the Table View to the View Controller. Continuing with this example, delegates can communicate with their delegating class  as long as they “speak” <...Delegate>.We can make our custom View Controller speaks Table View Delegate by adding  <UITableViewDelegate> to the top of our implementation file.

Screen Shot 2016-03-04 at 4.15.43 PM.png

Though as mentioned preciously, delegates can be of any type, hence the  id designation in a delegates property definition. This is how we are able to make custom View Controllers that performs very specific tasks and slap on a UITableView or a UIAlertController class, which are objects that perform very general, reusable tasks.

What’s the Protocol?

Put simply, a protocol is a list of methods that a delegate must implement if the methods are required (see lines 297 – 304) or may implement if the methods are optional (see lines 308 – 311. Additional protocols can be found in the UITableView.h file).

Screen Shot 2016-03-05 at 9.33.12 PM

Protocols can be thought of as rules on how a delegate and a delegating object will pass information to each other.As seen in the example above, the a View Controller can tell a Table View how many rows to present as well as what content will be displayed in each row. These two objects communicate with each other to present a series of rows on the View Controller. Objective-C is a single-inheritance language meaning a class can only inheret from one superclass, but delegation provides something similar to what multiple inheritance languages have in that it allows classes to access information and methods from a class other than its superclass.

By the People, For the People…

Delegation is a powerful tool that not only facilitates communication between objects. In its most simple form, it is a callback mechanism but delegation can also be used to support complex user interface elements like table views. Additionally, delegation allows us to split code up into two kinds of objects: the first being a fixed, reusable object and the second, a custom object capable of performing particular tasks. An example reusable objects are those found in the iOS API library (like UIAlertController). This allows us to write more abstracted code and creates greater reusability in objects with delegates. Delegation is an important tool to have in our toolbelts, and I hope some of the confusion about delegation has been cleared up in the above.

*Thank you to this article by Craig A. Will and this article by Alex E. Fish that helped clarify delegation for me.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s