Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocks vs Delegates [duplicate]

Possible Duplicate:
Do code blocks completely replace delegates?

I just encountered the following declaration from a forum:

"Delegates is the past. Blocks are the future."

1) Are blocks the preferred way to do 'delegation' duties over delegates?
2) Is there any particular benefit of using a delegate vs a block?

like image 335
Frederick C. Lee Avatar asked Nov 16 '12 17:11

Frederick C. Lee


People also ask

What is the difference delegates and callbacks?

Callbacks are similar in function to the delegate pattern. They do the same thing: letting other objects know when something happened, and passing data around. What differentiates them from the delegate pattern, is that instead of passing a reference to yourself, you are passing a function.

What is the difference between delegate and closure?

Difference between protocol Delegate and Closures –In the protocol method, we use the call by delegate method then use it but in closer, we can call it directly, there is no need for the delegate method.

When to use delegates and closures in swift?

My simple rule: if something requires one function as it's interface, a callback is usually a good solution. If more than one function is required, especially when they're required for the basic function of an object, a Delegate is probably a better solution.

What is difference between protocol and delegate in Swift?

Protocol is a set of methods (either optional or required) that would be implemented by the class which conforms to that protocol. While, delegate is the reference to that class which conforms to that protocol and will adhere to implement methods defined in protocol.


1 Answers

I think there's a slight misunderstanding in what delegates do and what blocks do.

In Objective-C, there are three ways to handle callbacks:

  1. Delegation -> where you make one object the delegate of another object and you have to specify which kinds of events generated by the "parent" object the delegate object will respond to.

  2. Target-Action -> typical in UI interactions, where a UI subview (button, slider, etc) generates an event based on some user input (for example a touch/tap) that is handled by a predefined event handler (typically some Objective-C method that the developer specifies).

  3. Notification -> where an object registers itself with an instance of NSNotificationCenter to "listen" for events of any type and responds to one or more of those events.

A block is not by itself a way to handle delegation, or any other callback.

They are self-contained pieces of code that have access to the local variables and parameters of the calling method. They can be used to define behavior in a bunch of different contexts. The main benefit of a block (as I see it) is that it can simplify code by eliminating extraneous overly-specific methods that would clutter your codebase. Blocks help to localize code to where it makes the most sense: right there within the callback mechanism.

Basically, using them enhances readability and makes code more maintainable.

Whether these benefits make blocks the 'preferred' method of handling callbacks is definitely a matter of personal opinion and experience. ;)

like image 115
Tony Armstrong Avatar answered Sep 18 '22 06:09

Tony Armstrong