President IOS Block Programming Guide 4 declaring and creating blocks

Source: Internet
Author: User
<span id="Label3"></p>Declaring and Creating Blocks (declaring and creating Blocks)<p><p></p></p>Declaring a block Reference (declares a block Reference)<p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;"><p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;">Block variables hold references to Blocks. You declare them the using syntax similar to the you use the declare a pointer to a function, except <code style="font-size: 13.3900003433228px; font-family: Courier,Consolas,monospace; color: #666666;">^</code> D of <code style="font-size: 13.3900003433228px; font-family: Courier,Consolas,monospace; color: #666666;">*</code> . The block type fully interoperates with the rest of the C type System. The following is all valid block variable declarations:</p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;">the <span style="font-size: 14px;">block variable maintains a reference to the BLOCK. You declare that block uses the same syntax as declaring a function pointer, except that you use "^" instead of "*". The block type can interoperate with all C type Systems. The following are the declarations of the block variables</span> <span style="font-size: 14px;">:</span></p></p><pre><pre><span style="color: #0000ff;">void</span> (^blockreturningvoidwithvoidargument) (<span style="color: #0000ff;">void</span><span style="color: #000000;">);</span> <span style="color: #0000ff;">int</span> (^blockreturningintwithintandchararguments) (<span style="color: #0000ff;">int</span><span style="color: #0000ff;">char</span><span style="color: #000000;">);</span> <span style="color: #0000ff;">void</span> (^arrayoftenblocksreturningvoidwithintargument[<span style="color: #800080;"></span>]) (<span style="color: #0000ff;">int</span>);</pre></pre><p><p></p></p><p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;"><p style="font-size:13px; margin-top:0px; margin-bottom:0.833em; >blocks also support variadic (<code style=" font-size:13.3900003433228px; font-family:courier,consolas,monospace; color: #666666; ">...) Arguments. A block that takes no arguments must specify <code style="font-size:13.3900003433228px; font-family:courier,consolas,monospace; Color: #666666; ">void </code>  in the argument list. </p></p><p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;"><p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;">Blocks is designed to being fully type safe by giving the compiler a full set of metadata to use to validate use of Blocks, Parameters passed to blocks, and assignment of the return Value. You can cast a block reference to a pointer of arbitrary type and vice Versa. You cannot, however, dereference a block reference via the pointer dereference operator ( <code style="font-size: 13.3900003433228px; font-family: Courier,Consolas,monospace; color: #666666;">*</code> )-thus a block ' s size cannot be computed at compile Time.</p></p><p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;"><p style="font-size: 13px; margin-top: 0px; margin-bottom: 0.833em;">You can also create types for blocks-doing and so are generally considered to being best practice if you use a block with a give N Signature in multiple places:</p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;"><span style="font-size: 14px;"> <span style="font-family: ‘Lucida Grande‘,‘Lucida Sans Unicode‘,Helvetica,Arial,Verdana,sans-serif;">blocks also supports variable Parameters. A block with no parameters must specify void in the parameter List. </span><br></span></p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;"><span style="font-family: ‘Lucida Grande‘,‘Lucida Sans Unicode‘,Helvetica,Arial,Verdana,sans-serif;"><span style="font-size: 14px;">Blocks is designed to be a complete security type for the compiler, it has a complete set of data sources to detect the legitimacy of the block, and by passing it to the blocks parameter, assign the return Value. You can create arbitrary pointer types to block, and vice versa (PS: this sentence, translation in doubt). however, You cannot use the dereference operator (*) to dereference a block--because it does not calculate the size of the block at compile Time.</span></span></p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;"><span style="font-family: ‘Lucida Grande‘,‘Lucida Sans Unicode‘,Helvetica,Arial,Verdana,sans-serif;"><span style="font-size: 14px;">You can also create a block as a type, which is usually the best approach when you want to use blocks with the same block signature in multiple Places.</span></span></p></p><pre><pre><span style="color: #0000ff;">float</span> (^myblocktype) (<span style="color: #0000ff;">float</span><span style="color: #0000ff;">float</span><span style="color: #000000;"></span><span style="color: #008000;">//</span> <span style="color: #008000;"> ...;</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> ... ;</span></pre></pre><p><p></p></p><p><p></p></p>Creating a block (create a block)<p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top:0px; margin-bottom:0.833em; >you use The <code style=" font-size:13.3900003433228px; font-family:courier,consolas,monospace; color: #666666; ">^  operator to indicate the beginning of a block literal Expression. It is followed by an argument list contained Within <code style="font-size:13.3900003433228px; font-family:courier,consolas,monospace; Color: #666666; ">()</code>. The body of the block is contained Within <code style="font-size:13.3900003433228px; font-family:courier,consolas,monospace; Color: #666666; ">{}</code>. The following example defines a simple block and assigns it to a previously declared variable (<code style="font-size:13. 3900003433228px; font-family:courier,consolas,monospace; Color: #666666; ">onefrom </code>)-here The block is followed by the Normal <code style="font-size:13.3900003433228px; font-family:courier,consolas,monospace; Color: #666666; ">;</code>  that ends a C statement. </p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;"><span style="font-size: 14px;">You use the ^ operation to indicate the beginning of a block Expression. There may also be a parameter list for the () Package. The body of the block is contained in {}. The following example defines a simple block assigned to an already existing variable (onefrom)-the block here is normal and is knot in the C language.</span></p></p><pre><pre><span style="color: #0000ff;">float</span> (^onefrom) (<span style="color: #0000ff;">float</span><span style="color: #000000;"></span>= ^ (<span style="color: #0000ff;">float</span> <span style="color: #000000;"> afloat) { </span><span style="color: #0000ff;">float</span><span style="color: #800080;">1.0</span><span style="color: #000000;">; </span> <span style="color: #0000ff;">return</span> <span style="color: #000000;">result;};</span></pre></pre><p><p></p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;">If you don ' t explicitly declare the return value of a block expression, it can be automatically inferred from the contents of the Block. If The return type <code style="font-size: 13.3900003433228px; font-family: Courier,Consolas,monospace; color: #666666;">void</code> is inferred and the parameter list are, then you can omit the <code style="font-size: 13.3900003433228px; font-family: Courier,Consolas,monospace; color: #666666;">(void)</code> parameter list as Well. If or when multiple return statements is present, they must exactly match (using casting if necessary).</p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;"><span style="font-size: 14px;">If you do not explicitly declare the return value of the block expression, the system can infer it based on the contents of the Block. If the return type is inferred and the parameter list is empty, then you can also omit the parameter List. If you need a lot of return values, they need to send a match (type conversion if necessary).</span></p></p><p><p><span style="font-size: 14px;"></span></p></p>Global Blocks (block)<p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;">At a file level, you can use a block as a global literal:</p></p><p style="margin-top: 0px; margin-bottom: 0.833em;"><p style="margin-top: 0px; margin-bottom: 0.833em;"><span style="font-size: 14px;">At the file level, you can use block as a global variable.</span></p></p><pre><pre><span style="color: #0000ff;">#import</span> <span style="color: #0000ff;">int</span> <span style="color: #800080;">0</span> <span style="color: #000000;">;</span> <span style="color: #0000ff;">int</span> (^getglobalint) (<span style="color: #0000ff;">void</span><span style="color: #0000ff;">return</span> globalint;};</pre></pre><p><p></p></p>Nextprevious<br><p><p>This article original, reproduced please indicate the source: http://blog.csdn.net/zhenggaoxing/article/details/44308855</p></p><p><p>President IOS Block Programming Guide 4 declaring and creating blocks</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.