Block in IOS and lambda and ioslamloud in C ++ 11

Source: Internet
Author: User

Block in IOS and lambda and ioslamloud in C ++ 11

Block in ios is a function pointer, but to be more precise, in fact, it should be regarded as object-c's support for lambda in C ++ 11 or a language variant. its actual content is the same, I have already introduced C ++ lambda. Now let's talk about block in ios.

The actual behavior of a Block is similar to that of a Function. The biggest difference is that the variable value of the same Scope can be accessed. The Block entity format is as follows:

^ (Input parameter column) {behavior subject };

The Block object starts with "^", followed by parameter columns wrapped in parentheses (such as int a, int B, int c), return a * ;};
This indicates that the Block will return the Square Value of the input value (int a is the parameter column, return a * a; is the behavior subject ). Remember to add ";" in the final part of the behavior subject, because it is a narration, and ";" must be added at the end of the entire {}, because the Block is an object. The usage is as follows:

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


Strange, right? 5 In the parentheses below will be treated as the input value of a, and then the result variable is output 5*5 = 25 through the Block.

Is there a simple way? Otherwise, why is it so long? Yes. Next, we will introduceBlock PointerTo simplify our writing.

Block Pointer is defined as follows:

Return Value (^ name) (parameter column );

For example:

  1. // Declare a square Block Pointer. The Block to which it points has an int input and int output.
  2. Int (^ square) (int );
  3. // Specify the Block object to square
  4. Square = ^ (int a) {return a * ;};
  5. // Call a method. Does it look like a function?
  6. Int result = square (5 );
  7. NSLog (@ "% d", result );

Is it easy?

You can also pass Block Pointer as a parameter to a function, for example:

  1. Void myFunction (int (^ mySquare) (int); // function Definition, Block as a parameter
  2. Int (^ mySquare) (int) = ^ (int a) {return a * a ;}; // defines a Block pointer variable of mySquare.
  3. MyFunction (mySquare); // use mySquare as a parameter of myFunction


The above three lines of code are equivalent to the following line of code:

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


When it is used as the input value of Object-C method, you need to write the type before the variable, and then add parentheses. For example:

  1. -(Void) objcMethod :( int (^) (int) square; // The type of the square parameter is int (^) (int)


Access variable

1. You can read the variable values of the same Scope as Block pointer:

  1. {
  2. Int outA = 8;
  3. Int (^ myPtr) (int) = ^ (int a) {return outA + ;};
  4. // The value of outA of the same type can be read in the block.
  5. Int result = myPtr (3); // result is 11
  6. NSLog (@ "result = % d", result );
  7. }


The following is an interesting piece of code:

  1. {
  2. Int outA = 8;
  3. Int (^ myPtr) (int) = ^ (int a) {return outA + a ;}; // The value of outA of the same type can be read in the block.
  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 performs a copy operation when the outA variable value is used in the main body to copy the outA value. Therefore, even if outA is replaced with a new value, the copy value in myPtr will not be affected.

Note that the copy value here is the value of the variable. If it is a memory location (address), in other words, this variable is a pointer,

Its value can be changed in the block. Example:

  1. {
  2. NSMutableArray * mutableArray = [NSMutableArray arrayWithObjects: @ "one", @ "two", @ "three", nil];
  3. Int result = ^ (int a) {[mutableArray removeLastObject]; return a * a;} (5 );
  4. NSLog (@ "test array: % @", mutableArray );
  5. }


The original value of mutableArray is {@ "one", @ "two", @ "three"}. After mutableArray is changed in the block, it becomes {@ "one ", @ "two.

2. directly access static variables

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

You can even directly modify the outA value in the block, for example, the following statement:

  1. {
  2. Static int outA = 8;
  3. Int (^ myPtr) (int) = ^ (int a) {outA = 5; return outA + ;};
  4. Int result = myPtr (3); // The value of result is 8, because outA is a static variable.
  5. NSLog (@ "result = % d", result );
  6. }


3. Block Variable Variables

If the modifier "_ block" is added before a variable (note that there are two underscores before the block), this variable is called block variable.

You can modify the value of this variable in the block as follows:

  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); // The result value is 5, and the num value is 6.
  6. Result = myPtr2 (0); // The result value is 6, and the num value is 7.
  7. NSLog (@ "result = % d", result );
  8. }


Because both myPtr and myPtr2 use the block variable of num, and the final value of num is 7.


The block in iOS can replace delegate

Yes !, If the value is simple, block is good.


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.