About the simple use of block body

Source: Internet
Author: User

1: What is block body, what is the form, and when is it used?

A) block is a C-level syntax and a feature of the runtime, similar to a function (function pointer) in standard C, which supports blocks very well starting with ios4.0.

b) Its form is as follows:

Again, block can access and block variables defined in the same lexical scope.

<span style= "" ><span class= "KWD" >{int</span><span class= "PLN" > Multiplier </span>< Span class= "pun" >=</span><span class= "PLN" > </span><span class= "lit" >7</span>< Span class= "pun" >;</span><span class= "PLN" ></span><span class= "KWD" >int</span> <span class= "PLN" > </span><span class= "pun" > (^</span><span class= "PLN" >myblock</ Span><span class= "pun" >) (</span><span class= "KWD" >int</span><span class= "pun" >) </span><span class= "PLN" > </span><span class= "pun" >=</span><span class= "PLN" > </span><span class= "pun" >^ (</span><span class= "KWD" >int</span><span class= "PLN" > Num</span><span class= "pun" >) </span><span class= "PLN" > </span><span class= " Pun ">{</span><span class=" PLN "> </span><span class=" KWD ">return</sPan><span class= "PLN" > num </span><span class= "pun" >*</span><span class= "PLN" > Multipiler</span><span class= "pun" >;</span><span class= "PLN" > </span><span class= "Pun" >}</span></span>
<span style= "" ><span class= "PLN" >printf</span><span class= "pun" > (</span><span class= "str" > "%d" </span><span class= "pun" >,</span><span class= "PLN" > Myblock</span ><span class= "pun" > (</span><span class= "lit" >3</span><span class= "pun" >)); </ Span><span class= "PLN" > </span><span class= "com" >//prints "+" </span></span>

}

The variable multiplier is within the same lexical scope as the defined myblock, so it can be accessed, but cannot be modified. (Block body can only modify global variables, static global variables, static local variables, __block modified local variables)

c) block in addition to the ability to define the parameter list, return type, but also to obtain the definition of the lexical scope of the state (such as local variables), and under certain conditions (such as using the __block variable) can modify these states. In addition, these modifiable states are shared among multiple blocks within the same lexical scope, and can continue to share or modify these states, even if the lexical scope (such as stack expansion, out of scope) is out. In general, blocks are encapsulated in short snippets of code that are used as work units, often for concurrent tasks, traversal, and callbacks.

Use of 2:block

A:block can not modify the global variables, only the local variable __block is only for the local variable to take effect of a variable storage type of the keyword, so the __block type of variables are stack volume;

B: Modifying local variables


C: modifying instance variables

3: Use instance

A: Local variables cannot be modified in black, but can be accessed



Visible global variable i, static global variable J, instance variable k,__block modified local variable l, static local variable m has been modified, and local variable n can also be accessed normally.

Two other related text links

http://blog.csdn.net/menxu_work/article/details/8762848

http://www.cnbluebox.com/?p=255

About the simple use of block body

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.