4 ms·
The way I explained delegates to a friend is: You have this friend, lets say his name is Steve. Steve is a really good guy, and usually excited to hear about w
by Zev 15y ago
The way I explained delegates to a friend is:
You have this friend, lets say his name is Steve. Steve is a really good guy, and usually excited to hear about what you're working on - and to help out, if he can. He also happens to be really smart and knows everything about everything. He read the dictionary when he was young and continues to read wikipedia daily. So you talk to him when you need to do something[1]. Or maybe you did something and are really excited about it[2]. Perhaps you're about to do something and just need that one last piece of the puzzle before you can finish it up[3]. And if you're unsure about if an idea is worth perusing, Steve is the right person to talk to[4].
Another reason why you're such good friends with Steve is that he is an animal lover and has promised that if you ever call on him to watch your dog because you just need to take a vacation for a weekend, he will watch your dog[5] for you. Since Steve is an animal lover, he might also be able to watch your pet ferret[6]. However, Steve is allergic to cats, and much to his dismay, he cannot watch your cat for you. But, you understand. You can post a notification on Twitter to ask all your other friends if any of them can watch your cat for you[7, 8].
Steve is a delegate. There's only one of him. You only need one.
[1-4]. Some examples of when you would use a delegate
5. Protocols can have some methods that their delegate is required to implement, and the compiler will error if they are not implemented.
6. Some methods are optional to implement.
7. Since a method can be optional, it might not be implemented.
8. Notifications are another way for objects in Objective-C to talk to one another. Instead of a 1:1 mapping, they are a 1:Many mapping.