IOS Block Usage (1)-once again

Source: Internet
Author: User

About BLOCK:

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 starts with "^", followed by a parameter column wrapped in parentheses (such as int a, int b, int c), the body of the action is wrapped in braces, and the exclusive name is called block literal. The behavior subject can return the value by returning, and the type will be automatically identified by compiler. If there are no parameter columns to write: ^ (void).

For example, here is an example:

[CPP]View Plaincopy
    1. ^ (int a) {return a*a;};


This is the square value that represents the value the block will return to (int A is the parameter column, return a*a; is the subject of the action). 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:

[CPP]View Plaincopy
    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:

[CPP]View Plaincopy
    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:

[CPP]View Plaincopy
    1. void myFunction (int (^mysquare) (int)); function definition, block as a parameter
    2. Int (^mysquare) (int) = ^ (int a) {return a*a;}; Define a mysquare block pointer variable
    3. MyFunction (MySQUARE); Take MySQUARE as the parameter of MyFunction


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

[CPP]View Plaincopy
    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:

[CPP]View Plaincopy
    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:

[CPP]View Plaincopy
    1. {
    2. int OutA = 8;
    3. Int (^myptr) (int) = ^ (int a) {return OutA + A;};
    4. The value of the same type of outa can be read in block
    5. int result = MYPTR (3); Result is 11
    6. NSLog (@ "result=%d", result);
    7. }


Let's take a look at some interesting code:

[CPP]View Plaincopy
    1. {
    2. int OutA = 8;
    3. Int (^myptr) (int) = ^ (int a) {return OutA + A;}; The value of the same type of outa can be read in 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 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:

[CPP]View Plaincopy
    1. {
    2. Nsmutablearray *mutablearray = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil];
    3. int result = ^ (int a) {[Mutablearray removelastobject]; return a*a;} (5);
    4. NSLog (@ "Test array:%@", Mutablearray);
    5. }


The value of the original Mutablearray is {@ "one", @ "one", @ "three"}, after the block is changed Mutablearray, it becomes {@ "one", @ "" "}.

2. Direct access to variables of type static

[CPP]View Plaincopy
    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:

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


3. Variables of Block variable type

If you add the modifier word "__block" before a variable (notice 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:

[CPP]View Plaincopy
    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); Result has a value of 5,num of 6
    6. result = MYPTR2 (0); Result has a value of 6,num of 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.

IOS Block Usage (1)-once again

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.