Essentially what I’m asking is, what is a delegate, but also HOW EXACTLY do I use one?
What is the flow of logic with delegates? I need things to be explained well and so for my many many google searches and searches on this site have revealed what they’re for, but not really how to use them.
I have a class that has a delegate in, and a function you can call asks for a delegate as an argument. What am I supposed to do, construct a duplicate delegate in my class, pass it through and then they’re linked? Some really basic explanation for dumb people would be much appreciated!
Thank you!
Basically delegates are variables of type id that are say “coupled” with a Protocol which is pretty much like a Java or C# interface except for the fact that protocols may specify optional methods that you do not have to implement.
So in order to use one the class that will be the delegate implements the protocol like this:
After that the class that has the delegate variable just has to be set usually like this:
After that the “delegate class” may call the implemented methods on the other class like this:
You can do this safely for any methods that are not below the keyword
@optional, for methods in the protocol declared below the keyword@optional, you should first check if the delegate decided or not to implement the method like this: