[Ios learning] 7. Blocks Introduction

Source: Internet
Author: User

BlocksBlocks is an extension of C language. blocks is an anonymous function with an automatic variable (local variable. This concept is: closure. For example, lambda in python has also introduced lambda in c ++ 11. In Wikipedia, Closure: Closure is short for Lexical Closure, is a function that references a free variable. This referenced free variable will exist with this function, even if it has left the environment where it was created. Therefore, there is another saying that a closure is an entity composed of a function and its reference environment. A closure can have multiple instances at runtime. Different reference environments and the same function combination can generate different instances. Blocks Syntax: ^ return value type parameter list expression ^ expression
^ Void (int I) {printf ("% d", I) ;}^ (int I) {printf ("% d", I );} ^{ printf ("123 ");}
Note that when you omit the return value type, what type is returned by return in your expression. When you do not apply parameters, the (void) parameter list can be omitted.
When using block, we can declare the block variable, which is the same as the function pointer in c:

int f(int a) {     return a;}int (*fa)(int) = &f;

In blocks, block refers to the block syntax in the source code, or the value generated by the block syntax.
int (^blk)(int);int (^blk)(int) = ^(int a){ printf("%d, a); }int (^blk1)(int) = blk;int (^blk2)(int);blk2 = blk1;

Void f (int (^ blk) (int); // pass block int (^ f () (int) to the function; // return block as the return value.
You can use typedef:
Typedef int (^ blk_t) (int); void f (int (^ blk) (int) corresponds to: void f (blk_t blk) int (^ f () (int )) corresponding to: blk_t f ();

Comparison function pointer:

Int (* ff (int) (int *, int );

This is a bit difficult to understand. We should look at it from the inside out:

Ff (int) declares ff as a function, which has an int parameter.

The return value of this function is int (*) (int *, int );

This is a pointer to a function. Let's change the form:

 

[Cpp]View plaincopy
  1. Typedef Int(* Func )(Int*,Int);
  2. Func ff (Int)
    Here we can see that there is only one difference between * and ^. It can be used as: Automatic variables, function parameters, static variables, static global variables, and global variables. Block variables can be used the same as other type variables in C. For example:
    typedef int (^blk_t)(int);     blk_t blk = ^(int count){return count;};    blk_t *blkptr = &blk;     (*blkptr)(10);

    Automatic VariableLet's take a look at the automatic variable: block expression intercepts the value of the used automatic variable: Save the instantaneous value of the automatic variable. Code explanation:
    int val = 0;void (^blk)(void) = ^{printf("%d", val);};val = 3;blk();

    At this time, the output is 0 rather than 3.
    _ Block
    Automatic variable value interception can only save values that execute block syntax instantly, and cannot be rewritten after saving. A compilation error occurs when you rewrite the intercepted automatic variables in the block. If you want to pay the value to the automatic variable declared outside the block syntax in the expression of the block syntax, you need to add the _ block specifier: _ block int a = 0 on this automatic variable; void (^ blk) (void) = ^ {a = 1 ;}; blk ();
    Obtained Automatic Variables
    id array = [[NSMutableArray alloc]init];void (^blk)(void) = ^{id obj = [[NSObject alloc]init];                         [array addObject:obj];};id array = [[NSMutableArray alloc]init];void (^blk)(void) = ^{array = [[NSMutableArray alloc]init];};

    The second part of the above two sections of code will have an error. The _ block operator should be added. The value assigned to the intercepted variable array produces a compilation error. However, using intercepted values does not cause any problems.
    Const char text [] = "hello"; void (^ blk) (void) = ^ {printf ("% c", text [2]);};
    Const char * text = "hello"; void (^ blk) (void) = ^ {printf ("% c", text [2]);}; the above two sections of code will show different results, and the first section will report an error, because the method for intercepting automatic variables does not implement interception of the c array.

    ----- 2014/3/18 Beijing

Related Article

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.