iOS Development, iOS Interview Study Notes, Swift

What are Delegates and Protocols?

As I am preparing for my first iOS job and studying for interviews. One area that I feel a bit intimidated by is the idea of protocols and delegates. They seem to always be connected but are they really can we use one without the other? I think we can but then why are they always talked about together?

I wanted to learn more about them and here is what I have found out. First and foremost you don’t need to be working on the delegate design pattern to use Protocols. Protocols can be adopted by a class, structure, or enumeration.

Protocols

So what is a Protocol? Well, a protocol is a set of rules that need to be followed by the adopting type. Let’s try to understand it in the big picture way. Let’s go back to high school. I was definitely not one of the popular kids. Maybe you were but for the sake of this example let’s say that we were in a popular group. So being in the popular group we didn’t want just anyone to be allowed in our group so we created a set of rules and a list of properties we want others to have.

allowedInPopularGroup {
   var notAFreshman: Bool
   var playsASport: Bool
   var ownsACar: Bool

   func howCoolAreThey(friends, hobbies, clothes) -> Int
}

Now let’s say that we are at a football game and we want to do some recruiting we can conform to our protocol and allow people to join our group if they meet our predetermined questions and properties.

Notice that we didn’t write out our method, all we did was say what was to be our input and our output. It is up to those doing the recruiting to figure out how the method will work as long as the input and output are the same.

Take a look at what Apple has to say about Protocols. They have some great information on the syntax and requirements for creating and implementing protocols.

Delegates

Delegates are very common, and once you understand what they are you can find them all over the place. The dictionary definition of a delegate is to: entrust (a task or responsibility) to another person, typically one who is less senior than oneself. This is pretty much on point to what the delegate design pattern is. Apple definitionDelegation 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.

Delegates are used to share the tasks with others, handing off some of its responsabilities. Two examples that just about any iOS developer has encountered is UITableView and UICollectionView. When using them we have been using delegation this whole time, deciding how cells should be created to the layout.

The main reason that protocols and delegatesa are often to be taught to be together is that delegation is usually performed by using delegate protocols, we don’t have to use protocols we can use closures, but more often than not we will see delegates being implemented with protocols.

Conclusion

Now you have a greater idea of what protocols and delegates are and you can check out apple’s documentation for the best way to use them in your code. But when it comes to interviews it’s always good to have a broad sense of what we are using so that we can best describe them to our interviewers.

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 )

Connecting to %s