I’m only really beginning IOS development but have a few years dev of ASP.net through C#. To be honest I’ve never had a real need to understand delegates / events etc. before, I know that I’m using them when programming web.forms but a lot of the functionality is taken care of by the framework, behind the scenes.
So now that I’m developing in IOS I’m forced to try to understand how they function (I’m presuming here that the theory of delegates / events is the same across languages, maybe I’m wrong). Anyway, the following line of code in IOS:
if ([self.delegate respondsToSelector:@selector(startImporting:)])
{
[self.delegate startImporting:self];
}
Am I right in thinking that, in pseudo code, it means something along the lines of:
If the method/class calling this method has a method in it called ‘startImporting’ then call the method ‘startImporting’ within the calling class.
Hope that’s clear. If that’s the case then would it essentially be the same as having a static method in C# that you could call with something like:
myImportClass.startImporting();
Presumably not, or that’s how it would be done. So, am I missing the whole point of delegates, their benefits etc? I’ve read what they are over and over and while it makes sense, it never clicks, I never (in web forms anyway) have really seen the benefit of using them.
This becomes increasingly important as I’m moving to using lambda expressions in .net and they’re closely linked to delegates in C# so while I can just start using them, I’d prefer to know why and what benefit delegates actually are.
The delegation pattern in Cocoa is used to inform (report progress, etc.) or query (ask for credentials, etc.) another object without knowing much about it.
Typically, you use a protocol to define what methods you will call on the delegate and the delegate then needs to conform to that protocol. You can also add methods that the delegate doesn’t need to implement (optional). When you do so, you’ll have to call -respondsToSelector:, because you don’t know whether the delegate wants the particular method to be called or not.
An example:
You have a class that produces something, let’s call it
Machineand a worker of the classWorker. The machine needs to be adjusted for the task:Now that we have the machine we want to produce a massive amount of
Stuff:Okay, we’re done. But how do we know when an unit of
Stuffhas been produced? We could have our worker constantly standing beside our machine and wait:Wouldn’t it be great if the machine did inform us automatically, when it’s done with producing an unit of
Stuff?Let’s add the
workeras a delegate ofmachine:When
Machineis done producing something, it will then call:The
workerwill then receive this method and can do something:}
Isn’t that much more efficient and easier for the worker? Now he can do something more productive than standing besides the machine while the machine is still producing. You could now add even more methods to
MachineDelegate:Delegates can also be used to change the behavior of an object without subclassing it:
I hope I could help you understand the benefit of abstracting your code using delegates a little bit.