Study hard and make Progress day by day.
Read Address: http://www.cocoachina.com/ios/20150927/13525.html
If there is infringement, please inform, delete immediately.
Black for excerpts, red for its own understanding.
1. Most delegate protocols have several sources.
For example, if the required blocks require different parameters, reuse makes no sense.
It's better to use delegate at this time.
It can be achieved by defining multiple blocks, and multiple blocks can also define different names. But if there are too many sources, it is more appropriate to use delegate.
2. An object can have only one delegate.
If an object is a singleton, do not use delegation.
There is only one delegate for each object, but if you need to deliver the message to multiple people, you need to set up the proxy every time, abide by the protocol, and implement the method to increase the code. In this case, if the block is used, the callback message will only go into the block of the most recently executed method, and the amount of code shrinks a lot.
3. The general delegate method will have a return value
If the object request has additional information, you should use the delegation
The block callback does not return the result with a return. Instead, it is returned by parameters, as it should be.
-(void) Dosomethingcomplete: (void(^) (nsdata *data)) complete
{
dispatch_queue_t queue = dispatch_queue_create("test", NULL);
dispatch_async(queue, ^{
nsurl *url = [nsurl urlwithstring:@ "Test"];
nsdata *data = [nsdata datawithcontentsofurl: url];
if(complete) {
Complete (data);
}
});
}
-(void) test
{
[self dosomethingcomplete: ^ (nsdata *data) {
NSLog(@ "%@", data);
}];
}
4. Process vs. Results (processes vs. Results)
Delegate's callbacks are more process-oriented, and block is result-oriented.
If you use block to request a potentially failed request, you should use only one block.
The block or delegate can actually be recalled to all stages of information, but at the beginning of the design, the block feel is used to simplify, omit the process, so block feel formally oriented results.
Beika the development of the choice block or delegate