Simple use of block

Source: Internet
Author: User

Code blocks are essentially similar to other variables. The difference is that the code block stores the data as a function body. With code blocks, you can pass in parameters as you would call other standard functions, and get the return value.

A code block is essentially a variable, except that the data it stores is a function body, so the name is its own type, and the value is the function body.

When you use a name, you do not need to store the data, just as a data type.

The stored data is a function body, so it can be divided into the case of the parameter and the non-parameter.

The use is simply as data type, and there is no special place.

The callback of the so-called code block is essentially the class B call method Method1 (block), Class A blockdata the value of the code block into the parameter block, (i.e., the so-called implementation in Class A), Class B uses Blockdata to pass specific parameters into the Blockdata To implement the function.

Class B does not need to know its specific value when using a block of code, but is used as a data type, the real value is in Class A, that is, the type is used first, the specific value is passed in, which is called the code block callback.

In addition to being able to define parameter lists, return types, blocks can also obtain state (such as local variables) within the lexical scope that is defined, and can modify these states under certain conditions, such as using the __block variable. 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.

The control of the block and the function pointer.



Int (*cfunc) (int a) function call
int result = Cfunc (10);
Int (^bfunc) (int a) function call
int result = Bfunc (10);

The caret (^) is the syntax tag for the block. The return value defined by our familiar parameter syntax specification and the body of the block (that is, code that can be executed). The syntax for assigning a block variable to a variable is explained:

Call the Block object variable as you call the function:
int result = Myblock (4); Result is 2.

typedef int (^myblock) (int);

Then Myblock can be used as a name for a data type:

Myblock Block;

parameter is a code block of nsstring*

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. void (^printblock) (NSString *x);
    2. Printblock = ^ (nsstring* str)
    3. {
    4. NSLog (@"print:%@", str);
    5. };
    6. Printblock (@"Hello world!");

The operating result is: Print:hello world!

 Recursive invocation of code blocks

Code blocks want to be called recursively, the block variable must be a global variable or a static variable, so that the code block variable is initialized when the program starts, and can be called recursively

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. static void (^ const blocks) (int) = ^ (int i)
    2. {
    3. if (i > 0) {
    4. NSLog (@"num:%d", I);
    5. Blocks (i-1);
    6. }
    7. };
    8. Blocks (3);

Run Print results:

Num:3

Num:2

Num:1

 Using local variables and global variables in code blocks

Global variables can be used and changed in code blocks

[CPP]View Plaincopy
  1. int global = 1000;
  2. int main (int argc, const char * argv[])
  3. {
  4. @autoreleasepool {
  5. void (^block) (void) = ^ (void)
  6. {
  7. global++;
  8. NSLog (@"global:%d", global);
  9. };
  10. Block ();
  11. NSLog (@"global:%d", global);
  12. }
  13. return 0;
  14. }

Run Print results:

global:1001

global:1001

Local variables can be used, but they cannot be changed.

[CPP]View Plaincopy
    1. int local = 500;
    2. void (^block) (void) = ^ (void)
    3. {
    4. local++;
    5. NSLog (@"local:%d", local);
    6. };
    7. Block ();
    8. NSLog (@"local:%d", local);

Changing the local variable compilation in the code block does not pass. How do you change local variables in a block of code? Precede a local variable with a keyword: __block

[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
      1. __block int local = 500;
      2. void (^block) (void) = ^ (void)
      3. {
      4. local++;
      5. NSLog (@"local:%d", local);
      6. };

Simple use of block

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.