Beika the development of the choice block or delegate

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.