How to use the block of iOS program syntax

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">In the current order Ioblock is iOS after 4.0 new program syntax, strictly speaking the concept of block is not the scope of the basic program design, for beginners is not very easy to understand, but after iOS SDK 4.0, block appears in almost all the new api, in other words, It is not possible to use the new features of the SDK 4.0 version without knowing the concept of block, so we have to be familiar with block usage</span></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">1. we'll start by knowing how to define a simple block</span></p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">1 No parameter no return value</span> <span style="color: #0000ff;">void</span> (^ MyBlock1) (<span style="color: #0000ff;">void</span><span style="color: #000000;">); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">2 parameter has no return value</span> <span style="color: #0000ff;">void</span> (^ MyBlock2) (<span style="color: #0000ff;">int</span> a,<span style="color: #0000ff;">int</span> <span style="color: #000000;"> b); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">3 parameters have a return value</span> <span style="color: #0000ff;">int</span> (^ MyBlock3) (<span style="color: #0000ff;">int</span> a,<span style="color: #0000ff;">int</span> b);</pre></pre><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">2, the declaration of block, How do we really realize him? Let's explore how to implement a functional block, we take the first example</span></p></p><pre><pre> <span style="color: #0000ff;">void</span> (^ MyBlock1) (<span style="color: #0000ff;">void</span>) =^ (<span style="color: #0000ff;">void)</span><span style="color: #000000;">{ NSLog (</span><span style="color: #800000;">@ "</span> <span style="color: #800000;">This is my first block</span><span style="color: #800000;">"</span><span style="color: #000000;">); };</span></pre></pre><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">So we succeeded in achieving a bock, the above is for a block with no return value, for the return value of how to implement it, similar to the function, you just return at the end, for example, we use a third distance, the return of two number</span></p></p><pre><pre> <span style="color: #0000ff;">int</span> (^ MyBlock3) (<span style="color: #0000ff;"></span> int a,<span style="color: #0000ff;">int</span> b) =^ (<span style="color: #0000ff;">int</span> a,<span style="color: #0000ff;">int</span> <span style="color: #000000;"> b) { </span><span style="color: #0000ff;">return</span> A +<span style="color: #000000;">b; };</span></pre></pre><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">3, above we completed the definition and implementation of block, we will consider how to use him, in fact, the use of Bock is very simple, we just need to follow the use of the function as you can, as shown below</span></p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">Call Block1 (no return value no Parameter)</span> <span style="color: #000000;"> MyBlock1 (); </span> <span style="color: #008000;">//</span> <span style="color: #008000;">Call Block3 (there is a return value with Parameters)</span> <span style="color: #0000ff;">int</span> value= MyBlock3 (<span style="color: #800080;">ten</span>)<span style="color: #800080;"></span>;</pre></pre><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">4, block as a parameter, in the development of the app you will find a lot of cases are block as a parameter, which is also one of the extensive application of block, below we see how to let block as a parameter, now we define the dog object, the dog object has properties and behavior, as follows</span></p></p><p><p></p></p><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">At the same time define a master class, the owner has a dog, the owner can be issued instructions to the dog, the dog according to different instructions to perform different operations, here we take the form of block, the instruction through the block to pass information, at this time we need to use the block as a parameter:</span></p></p><p><p><span style="font-family: Microsoft YaHei;"></span></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="font-family: Microsoft YaHei; font-size: 14px;">The implementation section is as follows</span></p></p><p><p></p></p><p style="margin-left: 30px;"><p style="margin-left: 30px;"><span style="font-family: Microsoft YaHei; font-size: 14px;">In the main function, we can refer to it as follows</span></p></p><pre><pre> Host * Host=<span style="color: #000000;">[[host alloc] init]; Dog </span> * Dog=<span style="color: #000000;">[[dog alloc] init]; Dog.name </span> =<span style="color: #800000;">@ "</span> <span style=" color: #800000; ">dahuang </span> <span style="color: #800000;"> "</span> <span style=" color: #000000; ">; Host.dog </span> =<span style="color: #000000;">dog; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">0 encapsulates the directive into a block and then passes the information </span> <span style="color: #0000ff;">void </span> (^ mydog) (dog *dog) = ^ (dog *<span style="color: #000000;">dog) {[dog run]; [dog canbark]; [dog speakname]; }; </span> <span style="color: #008000;">//</span> <span style="color: #008000; >1 tell the dog what to do </span> <span style=" color: #000000; "> [host askdog:mydog]; </span> <span style="color: #008000;">//</span> <span style="color: #008000;">2 Let the dog do </span> [host toDo]; </pre></pre><p><p><span style="font-family: Microsoft YaHei; font-size: 14px;">5, __block keyword, block can be passed as parameters, so the execution time of block may not be very certain, so when we need to be in the block, the execution of the results assigned to the outside of the variable, we need to pay attention to, if you want to assign the result of the block to local variables, The error occurs because your local variables may have been destroyed when you execute the block, and all blocks cannot be assigned directly to local variables, Although global variables are possible. Block also introduces another keyword that transforms the life cycle of a local variable into a global variable, so that we can assign a value to the local variable as Follows:</span></p></p><pre><pre> <span style="color: #0000ff;">int</span> sum=<span style="color: #800080;">0</span><span style="color: #000000;">; </span> <span style="color: #0000ff;">void</span> (^ Myblock) (<span style="color: #0000ff;">int</span>,<span style="color: #0000ff;">int</span>) =^ (<span style="color: #0000ff;"></span> int a,<span style="color: #0000ff;">int</span> <span style="color: #000000;"> b) { sum</span>=a+<span style="color: #000000;">b; };</span></pre></pre><p><p></p></p>Jerry Education<br>Source:<span style="color: #000000;"><span style="color: #000000;"><span style="color: #000000;">http://www.cnblogs.com/jerehedu/</span></span></span><br><span style="font-family: ‘Microsoft YaHei‘; color: #000000;"><span style="font-family: ‘Microsoft YaHei‘; color: #000000;">Copyright Notice: The copyright of this article is <span style="color: #000000;">Yantai Jerry Education Technology co., Ltd.</span> and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.<br>Technical Consultation:</span></span><p><p>How to use the block of iOS program syntax</p></p></span>

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.