<span id="Label3"></p><p><p><br></p></p><p><p>This is my original written in the OC on the agreement and the agent article, suggest that you read this article first read this article, easy to understand: iOS protocol and delegate detailed (a) iOS protocol and delegate detailed (ii)</p></p><p><p><br></p></p><p><p>The official use of block is:</p></p><p><p><br></p></p><p><p><span style="font-size:11pt; font-family:MyriadSet"><strong>You can use the blocks to compose function expressions The can is passed to API, optionally stored, and Usedby multiple Threa Ds. Blocks is particularly useful as a callback because the block carries both the code to beexecuted on callback and the DAT A needed during that Execution.</strong></span></p></p><p><p><span style="font-size:11pt; font-family:MyriadSet"><strong><br></strong></span></p></p><p><p><span style="font-family:MyriadSet; font-size:11pt"></span> You can use the blocks composition method expression to make up the API interface, the choice of storage, or the use of it in different threads,<strong>blocks is very important in callbacks because blocks carries the code that needs to be recalled and the parameters at execution time</strong></p></p><p><p><strong></strong></p></p><p><p><strong><span style="font-size:14px">as can be seen from the above,</span></strong></p></p><p><p><strong><span style="font-size:14px">The most important part of blocks is that it can be used for callbacks, and, exactly, for a convenient callback.</span></strong></p></p><p><p><strong></strong> If you read the "Protocol and delegate" that I wrote before, you will know that Protocol (protocol) can be seen as a collection of methods, and blocks can be seen as a Protocol (protocol) with only one method of determining the Name.</p></p><p><p>In other words, block can be said to be a nonstandard protocol, similar to our verbal agreement</p></p><p><p>however, It is such a non-standard protocol, but it is very convenient for the development of our program, imagine, if I go to the roadside stalls to buy a popsicle, I and the stall also signed a sale agreement? no, at least I haven't seen it.</p></p><p><p>If it is really necessary to implement the above-mentioned trading process by means of the agreement, that is, the definition of a purchase and sale agreement, which has a method, the value of the money, the return value of the commodity object, and then the vendor implementation of this protocol, I can call the stall owner of this protocol method, finally smooth eat on my popsicle</p></p><p><p>You might think, it's too much trouble, it's just anti-human, so we can let the stall owner implement an incoming value for money, The return value is the commodity object of the block Object. Then I'm going to call this Object.</p></p><p><p>In other words, we do not have to write a protocol when the standard, in the class implementation of this protocol, only need to write a blocks method object, and then directly call to</p></p><p><p><br></p></p><p><p>Say so much, maybe you don't understand blocks, okay, let's start with the basics</p></p><p><p></p></p><p><p><span style="font-size:14px">Block's Basic Application:</span></p></p><p><p><span style="font-size:14px"></span> ①. define and use Blocks:</p></p><p><p></p></p><p><p>You use the ^ operator to declare a block variable and to indicate the beginning of a block literal. The body of the block itself is contained within {}</p></p><p><p></p></p><p><p>You can use the ^ operator to define a block variable, and the ^ operator appears at the beginning of the BLOCK. The body of the block is inside {}.</p></p><p><p>Example:</p></p><p><p>int multiplier = 7;<br>int (^myblock) (int) = ^ (int Num) {<br>Return num * multiplier;<br>};</p></p><p><p>Direct use:</p></p><p><p>printf ("%d", Myblock (3));<br>Prints "21"</p></p><p><p>Note that this use is directly using Myblock as a pointer to the normal method, you may have doubts, I only passed a parameter 3 to Myblock (that is, the num parameter in Blocks) ah, where is multiplier? Multiplier on top of MYBLOCK. The following is an explanation in the API Documentation:</p></p><p><p>Notice that, The block is able to make use of variables from the same scope in which it was Defined.</p></p><p><p>So myblock can use the multiplier variable directly</p></p><p><p>Here is an example diagram of the block above:</p></p><p><p></p></p><p><p>②. use blocks directly to make Callbacks:</p></p><p><p>The method that defines and uses blocks above is not much to say, defined in its own class and used Blocks) is not often used, we use blocks is generally used as a callback</p></p><p><p>Use one of the examples from the delegate I wrote Earlier: IOS protocol and delegate (ii) we use blocks to solve the "little lee buy tea" example.</p></p><p><p>This is the problem we need to solve:</p></p><p><p>for <span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">example, There is a leader, next to his secretary Xiao li, one day, the leader to Xiao Li Said: "xiao li ah, no tea, you buy me some good tea,</span> <span style="font-family:Arial; font-size:14px; line-height:26px; color:rgb(255,0,0)">if I buy it back, i'll give you a raise.</span> <span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span> <span style="font-family:Arial; font-size:14px; line-height:26px; color:rgb(51,51,255)">If you don't buy it, go and stand wall</span> . <span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">. "</span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span> in the above questions, Xiao Li needs to have a way to buy tea, but after the tea to buy back? Xiao Li will buy tea back after the event is the leadership to define, for example, "buy back, I give you a raise salary", this is the leadership of the development, Xiao Li just execute this method,</p></p><p><p>so, Xiao Li needs to have three methods, one is the success of the tea to buy back the event method, one is not tea to buy back the event method, there is a way to buy their own tea, and these event methods, can be directly written in the method of buying tea</p></p><p><p>General callbacks are used for time-consuming operations, and the method of callback during time-consuming operations can generally be seen as an event. For example, The time-consuming operation of the online request, the successful request for a callback after the success event (getsuccessevent), The handout is added block after the Blcoks reference, I am not very recommended to do So. I recommend adding an event to the back of all callbacks, not blcok, so it's easier for you and others to understand and maintain<br></p></p><p><p>The code below</p></p><p><p>Xiao Li is the object of the worker class, whose. m file is;</p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import "Worker.h" @implementation worker/** to buy Tea. callback event: buysuccessevnet: event that will be recalled if the tea is purchased successfully Buyfailevnet : If the purchase of tea fails back callback after the event */-(void) buyteaandsetbuysuccessevent: (void (^) ()) buysuccessevnet Setbuyfailevent: (void (^) ()) buyfailevnet{ NSLog (@ "buying tea"); Simulate the time-consuming operation of buying tea [nsthread sleepfortimeinterval:3.0]; NSLog (@ "i bought tea successfully"); If the caller implements this blocks, then callback if (buysuccessevnet) { buysuccessevnet (); } } @end</pre></pre><br><p><p></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">The. h file is:</span></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import <Foundation/Foundation.h> @interface worker:nsobject{ }/** to buy Tea. callback Event: Buysuccessevnet: if the purchase of tea is successful, the callback event buyfailevnet : If the purchase of tea fails back callback event */-(void) buyteaandsetbuysuccessevent :(void (^) ()) buysuccessevnet setbuyfailevent: (void (^) ()) buyfailevnet; @end</pre></pre><br><br><p><p></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span> We temporarily do not write the leadership of the leader class, for the moment we as a leader, directly in the main method calls Xiao Li's method</p></p><p><p>Here is the method called (which can be written in the Viewdidload method in viewcontroller):</p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc">-(void) viewdidload { [super viewdidload]; Worker *xiaoli = [[Worker alloc] init]; Call Xiao Li's method of buying tea [xiaoli buyteaandsetbuysuccessevent:^{ NSLog (@ "xiao li You do good, this month gives you a raise salary"); } setbuyfailevent:^{ NSLog (@ "xiao Li What do you think all day, go to the corner stand!");} ]; }</pre></pre><br>After execution, the output statement is:<p><p></p></p><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:‘Heiti SC Light‘"><strong><span style="color:#ff6666">are buying tea</span></strong></p></p><br><span style="color:#333333"><span style="color:#333333"></span></span><p style="font-family:Menlo; font-size:11px; line-height:26px; margin-top:0px; margin-bottom:0px"><p style="font-family:Menlo; font-size:11px; line-height:26px; margin-top:0px; margin-bottom:0px">After three seconds, it appears:</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"></p></p><p style="font-size:11px; line-height:26px; font-family:‘Heiti SC Light‘; margin-top:0px; margin-bottom:0px"><p style="font-size:11px; line-height:26px; font-family:‘Heiti SC Light‘; margin-top:0px; margin-bottom:0px"><strong><span style="color:#ff6666">I got the Tea.</span></strong></p></p><p style="font-size:11px; line-height:26px; font-family:Menlo; margin-top:0px; margin-bottom:0px"><p style="font-size:11px; line-height:26px; font-family:Menlo; margin-top:0px; margin-bottom:0px"><span style="font-family:‘Heiti SC Light‘"><strong><span style="color:#ff6666">Xiao Li You do well, this month you pay a raise</span></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><br></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px">We can carefully analyze the above code, the above code does not directly define a block variable, but in the method directly declare</p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><br></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px">There is one aspect to note <span style="color:#330033">, that is, if this method is mine, I declare a block in this method, then I just need to invoke (in fact, the call is a callback)</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033">And if I call this method, this method needs to pass in the block, then I need to implement this block, although this block I do not define the variable, I just need to write the body of the block can be</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033">This is an example of using block directly for callbacks, and the <span style="color:rgb(51,0,51)">way</span> we often use blocks in a project</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"><span style="color:rgb(51,0,51)"><br></span></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"><span style="color:rgb(51,0,51)">③. defining and using blocks for callbacks</span></span> <span style="color:rgb(51,0,51)">:</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:rgb(51,0,51)">Some people may ask, since I can use <span style="color:rgb(51,0,51)">blocks directly to make callbacks, then I can define a blocks, and then use this <span style="color:rgb(51,0,51)">blocks to do callbacks, of course, but it is very not recommended to do so, there are two problems</span> </span></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:rgb(51,0,51)"><span style="color:rgb(51,0,51)"><span style="color:rgb(51,0,51)"></span></span></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:rgb(51,0,51)"><span style="color:rgb(51,0,51)"><span style="color:rgb(51,0,51)">1. Code more, do not use maintenance, understand</span></span></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:rgb(51,0,51)"><span style="color:rgb(51,0,51)"><span style="color:rgb(51,0,51)">2. There can be a problem with a null pointer, that is, the callback is empty</span></span></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033">The following code is written in this way:</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033">Worker's. h file:</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"></span></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import <Foundation/Foundation.h> @interface worker:nsobject{ the way to buy tea */-(void) buytea;// If the purchase of tea is successful, it will callback the Event//remember to use the Copy property reference: http://blog.sina.com.cn/s/blog_6dce99b10101bsum.html@property (copy) void (^ Buysuccessevnet) (void), or//@property (copy) void (^buyfailevnet) (void) of the callback event if the tea is purchased successfully; @end</pre></pre><br><br><p><p></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033">. M file:</span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"><br></span></p></p><p style="margin-top:0px; margin-bottom:0px"><p style="margin-top:0px; margin-bottom:0px"><span style="color:#330033"></span></p></p><pre name="code" class="objc"><pre name="code" class="objc">#import "Worker.h" @implementation worker/** to buy tea method */-(void) buytea{ NSLog (@ "buying tea"); Simulate the time-consuming operation of buying tea [nsthread sleepfortimeinterval:3.0]; NSLog (@ "i bought tea successfully"); If the caller implements this blocks, then callback if (self.buysuccessevnet) { self.buysuccessevnet (); } } @end</pre></pre><br><p><p></p></p><span style="color:#333333"><span style="color:#333333"></span></span><p><p><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:26px">Called in the main method:</span></span></p></p><p><p><span style="font-family:Arial; color:#333333"><span style="font-size:14px; line-height:26px"></span></span></p></p><pre name="code" class="objc"><pre name="code" class="objc"> Worker *xiaoli = [[Worker alloc] init]; Call Xiao Li's method of buying tea [xiaoli buytea];</pre></pre><br>I don't know if you found Out. no, this time the problem arises, because there is no set of Xiaoli success and failure events, so the direct call is not implemented at All. This is a very deadly problem in teamwork, because other people are not quite sure how the worker you write works, so the call didn't set its blocks, so there will be problems that confuse Others. Here's How to do it:<p><p></p></p><p><p></p></p><p><p>Only these results will appear:</p></p><p><p><br></p></p><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:‘Heiti SC Light‘"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:‘Heiti SC Light‘"><strong>are buying tea</strong></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><strong><br></strong></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><strong></strong> <span style="font-family:‘Heiti SC Light‘"> <strong>I got the Tea.</strong></span></p></p><br><p><p>The code that is called correctly is:</p></p><p><p><br></p></p><p><p></p></p><pre name="code" class="objc"><pre name="code" class="objc"> Worker *xiaoli = [[Worker alloc] init]; [xiaoli setbuysuccessevnet:^{ NSLog (@ "xiao li You do good, this month gives you a raise); }]; [xiaoli setbuyfailevnet:^{ NSLog (@ "xiao Li What do you think all day, go to the corner stand!"); }]; Call Xiao Li's method of buying tea [xiaoli buytea];</pre></pre><br><br><p><p></p></p><p><p>The result is:</p></p><p><p></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><span style="font-family:‘Heiti SC Light‘"><strong>are buying tea</strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><span style="font-family:‘Heiti SC Light‘"><strong>I got the Tea.</strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><strong></strong> <span style="font-family:‘Heiti SC Light‘"> <strong>Xiao Li You do well, this month you pay a raise</strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><span style="font-family:‘Heiti SC Light‘"><strong><br></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><span style="font-family:‘Heiti SC Light‘"><strong></strong></span></p></p><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><p style="margin-top:0px; margin-bottom:0px; font-size:11px; font-family:Menlo"><span style="font-family:‘Heiti SC Light‘"><strong>so, It is not very recommended to use this method, the code is complex and not straightforward enough, it is easy for users to feel confused</strong></span></p></p><span style="font-family:‘Heiti SC Light‘"><span style="font-family:‘Heiti SC Light‘"><strong><br></strong></span></span><br><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><br></span></p></p><p><p><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span></p></p><p><p></p></p><p><p><br></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>IOS Block Detailed</p></p></span>
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