The lambda in block and c++11 in iOS

Source: Internet
Author: User

The block in iOS can be said to be a function pointer, but to be more precise, it should actually be the support of object-c to Lambda in c++11 or a variant of the language, the actual content is the same, C + + lambda I have a brief introduction, Now the block in iOS

Block's actual behavior and function are similar, the biggest difference is that the variable value can be accessed in the same scope. The block entity form is as follows:

^ (incoming parameter column) { behavior body};

The block entity begins with "^", followed by a parameter column wrapped in parentheses (such as int a, int b, int c),return a*a;
This is the square value that represents the value that the block will returnto (int A is the parameter column, return a*a; is the behavior body). Remember to add ";" in the subject, because it is a narrative, and the whole {} is finally added ";" because block is an object entity. Use the following:

    1. int result = ^ (int a) {return a*a;}  (5);
    2. NSLog (@"%d", result);


That's weird, isn't it? The 5 in the back parenthesis is treated as the input value of a, which is then assigned to the result variable via block output 5*5 = 25.

Is there a simple way, eh? Or are you going to write this long? Yes. The next step is to introduce something called Block Pointer to simplify our writing.

The Block pointer is defined like this:

Callback Value (^ name) ( parameter column);

For example, the following:

    1. Declares a square block Pointer that points to a block with an int input and an int output
    2. Int (^square) (int);
    3. Assigning a block entity to square
    4. Square = ^ (int a) { return a*a;};
    5. Call method, feeling is not much like the use of function?
    6. int result = Square (5);
    7. NSLog (@"%d", result);

Is it easy?

You can do it. Block pointer is passed as a parameter to a function, such as:

    1. void MyFunction (int (^mysquare) (int)); The definition of//function, the block as a parameter
    2. Int (^mysquare) (int) = ^ (int a) {return a*a;}; //define a mysquare block pointer variable
    3. MyFunction (MySQUARE); //MySQUARE as parameters of MyFunction


The above three lines of code are actually equivalent to this line of code:

    1. MyFunction (^int (int a) {return a*a;});


When used as an incoming value for Object-c method, the type needs to be written in front of the variable, followed by parentheses. For example, the following is the wording:

    1. -(void) Objcmethod: (int (^) (int)) square; the type of the//square parameter is int (^) (int)


Access variables

1, can read and block pointer the same scope variable value:

    1. {
    2. int OutA = 8;
    3. Int (^myptr) (int) = ^ (int a) { return OutA + A;};
    4. //block can read the value of the same type of Outa
    5. int result = MYPTR (3); //Result is one
    6. NSLog (@"result=%d", result);
    7. }


Let's take a look at some interesting code:

  1. {
  2. int OutA = 8;
  3. Int (^myptr) (int) = ^ (int a) { return OutA + A;}; //block can read the value of the same type of Outa
  4. OutA = 5; //Change the value of Outa before calling Myptr
  5. int result = MYPTR (3); //The value of result is still 11, not 8
  6. NSLog (@"result=%d", result);
  7. }


Why is the value of result still 11? Instead of 8? In fact, myptr a copy of the value of the outa used in its subject , copy the value of Outa. So, after Outa even replaced with a new value, there is no effect on the value of copy in Myptr.

It is important to note that the copy value here is the value of the variable, if it is a memory location (address), in other words, the variable is a pointer,

Its value can be changed in the block. Here's an example:

    1. {  
    2.     nsmutablearray  *mutablearray = [nsmutablearray arraywithobjects:@ "one",  @
    3.     int result = ^ ( int a) {[Mutablearray removelastobject]; return a*a;} (5);   
    4.     nslog (@
    5. }  


The value of the original Mutablearray is {@ "one", @ "one", @"three"}, after being changed in the Block Mutablearray, it becomes {@ "a", @ "both"}.

2. Direct access to variables of type static

    1. {
    2. static int outA = 8;
    3. Int (^myptr) (int) = ^ (int a) {return OutA + A;};
    4. OutA = 5;
    5. int result = MYPTR (3); the value of//result is 8 because Outa is a variable of type static
    6. NSLog (@"result=%d", result);
    7. }

You can even modify the value of the Outa directly inside the block, such as the following:

    1. {  
    2.      static INT OUTA = 8;  
    3.     int  (^MYPTR) ( int)  = ^ (int a) { outa = 5;  return outa + a;};   
    4.     int  Result = myptr (3);   //result is 8, because Outa is a variable of type static   
    5.     nslog (@
    6.       
    7. }  


3. Variables of Block variable type

If you add the modifier word "_ _block" before a variable(note that there are two underscores in front of the block), this variable is called block variable.

The value of this variable can be arbitrarily modified within the block, as in the following code:

  1. {
  2. __block int num = 5;
  3. Int (^myptr) (int) = ^ (int a) {return num++;};
  4. Int (^MYPTR2) (int) = ^ (int a) {return num++;};
  5. int result = myptr (0); A value of //result of 5,num is 6
  6. result = MYPTR2 (0); A value of //result of 6,num is 7
  7. NSLog (@"result=%d", result);
  8. }


Because both myptr and MYPTR2 are useful to num, the block variable, the final num value is 7.

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.