Reputation: 768
I created a protocol that requires the class client to implement a method. In the client class I also need to perform the same actions defined in this method not only when the delegate calls it. I don't think it would be a good idea to copy-paste the code, but I don't know either if it's a good practice to call this method directly within the client class. I was thinking that maybe the delegate methods should be called only by the delegate.
Should I create a third method that it's called inside the delegate method and when I need it? or, can I call the delegate method directly?
Upvotes: 3
Views: 441
Reputation: 23548
use blocks
instead of delegates
to solve your problem. Blocks do the same work as delegates, only it's far cleaner, requires less plumbing work (think about all those instance variables/properties you gotta shuttle between classes and their delegates.. in blocks, all that info is encapsulated within the block), and is consistent with the direction the iOS/objective-c community is moving in (you'll find a lot of high profile libraries evolving to substitute delegation and other stuff with blocks).
If you're not familiar with blocks or you find it's quirky syntax annoying, here is a user friendly guide.
Also here is a nice answer that compares a delegate
based solution with a block
based solution, and which illustrates how the a block solution is cleaner than a delegate one.
Upvotes: 2
Reputation: 4061
It's not necessary to create a third method, but it would be a good practice.
Let's assume you need to make certain checks for some kind of delegate call, then your code will be filled with lots of if-then-else statements. It would be better to have repeated-but-simple code than a messed-up code.
Upvotes: 1