Delegates and protocols:

I am learning delegates and protocols but it does not make sense to me completely, what should I have to do?

Hi @salman123,
First congratulations on learning Swift.

To the question you posed, with delegates and protocols, I have a quick question for you, is Swift your first language? or have you developed before with other languages? The answer to that helps to align concepts with something you might have learned earlier.

There are a couple of videos on this site and you can also refer to the videos here https://www.raywenderlich.com/108816/top-10-wwdc-2015-videos specifically to the 2nd video.

a simple way to understand the underlining base concept about delegates and protocols is

DELEGATE → This is authorization to a class/object to behave/interact as expected. So, with a TableView, you set the delegate for the DataSource to the class you create. This tells the TableView that when it requires a particular function, the delegate will supply that, like numberOfRowsInSection etc. However, what happens if the delegate does not have that function?? It could lead to a crash or some other problems.

PROTOCOL → Protocols are also called Interfaces in some other languages and/or an Abstract class. This is simply saying that this is the template and every class/structure that will inherit from this will have these functions, methods etc. So when you inherit your class with the protocol UITableViewDataSource, xcode will tell you that you have not written the code for the three functions that are expected to be present.

Protocols are becoming more popular and widely used, which we can discuss if you have more questions.

cheers,

Jayant

cheers,

Jayant

@salman123 Thanks very much for your question!

Delegates and Protocols are major concepts in learning iOS and Swift development, so don’t feel too bad if you’re struggling with it. However, rest assured it’s not impossible to understand, and you’re iOS journey will become much easier once you overcome this hurdle :slight_smile:

That being said:

In order to understand “delegates”, forget programming for a moment, and simply look at the word linguistically. What do you do when you delegate something? You are handing off a responsibility or task to someone else. You assign someone else to do some work for you. In programming, this is exactly the same.

@jayantvarma Did an excellent job by using UITableView as an example, since this is a fundamental component in iOS development that uses the “delegate” pattern.

Your ViewController assigns itself to be the delegate for the UITableView. What does this mean? It means that the UITableView has assigned the ViewController to do certain tasks on its behalf. Which tasks? To implement the required methods defined in the protocol which it is implementing. In the case of implementing the DataSource protocol for the UITableView, The ViewController is required to implement the “cellForRowAtIndexPath” and “numberOfRowsInSection” methods defined by the protocol.

So now your next question is: What is a protocol?

Again, @jayantvarma explanation above is correct. In my example, the protocol is an “agreement” or “contract” that the “delegate” promises to uphold. A real life example would be a Pizzeria. The driver would be the “delegate” for the Pizzeria who assigns the task of delivering the pizza to the driver. The protocol that the driver has to implement or the “contract/agreement” the driver has to uphold is to deliver the pizza on time, and collect payment.

So the ViewController which assigns itself to be the delegate for the UITableView must follow the “agreement/contract” required by the UITableView, which would be to implement the protocol methods specified by the UITableView.

I hope this helps!

All the best!

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.