IOS learning Block

Source: Internet
Author: User

I. Capture automatic variable values

// Capture the automatic variable value int val = 132; const char * FMt = "Val = % d \ n";/* in blocks, the block expression uses the automatic variable Val before it is declared. The block expression intercepts the value of the used automatic variable, that is, the instantaneous value of the automatic variable is saved. Because the block expression saves the value of the automatic variable, after the block syntax is executed, even changing the value of the automatic variable used in the bock does not affect the value of the automatic variable during block execution. Therefore, the two outputs are the same: val = 132 */void (^ BLK) (void) = ^ {printf (FMT, Val) ;}; BLK (); val = 12345; BLK ();

2. _ block specifiers

// _ Block specifier/* In fact, automatic variable value interception can only be saved in the moment when the block syntax is executed. After saving, the value cannot be rewritten. If you try to change the value of the intercepted automatic variable in the block, see what the result will be */INT val = 0; void (^ BLK) (void) = ^ {val = 1 ;}; BLK ();

The following error message is displayed:

How can this problem be solved? -------- Use the _ block Operator

// If you want to assign 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 before the automatic variable, you can assign values to the block. _ Block int val = 0; void (^ BLK) (void) = ^ {val = 1; nslog (@ "Val = % d", Val );}; BLK ();

Iii. Intercepted automatic Variables

As mentioned above, if the value is assigned to the automatic variables in the block, a compilation error will occur. So for intercepted objective-C objects, will such compilation errors occur when calling the method to change the object?

See the following example:

id array = [[NSMutableArray alloc]init];        void (^blk)() = ^{            id obj = [[NSObject alloc] init];            [array addObject:obj];        };        blk();

This is okay. First, the block intercepts the nsmutablearray Class Object's struct instance pointer. Here we use the intercepted value, so there is no problem, however, if a value is assigned to the intercepted variable, a compilation error occurs. Take a look at the following example:

id array = [[NSMutableArray alloc]init];        void (^blk)() = ^{            array = [[NSMutableArray alloc]init];        };        blk();

A compilation error prompt will appear at this time!

Obviously, the _ block operator must be appended to the intercepted automatic variables to solve the problem.


4. In addition, you must use the pointer carefully when using the C language array. For example:

const char text[] = "hello world";        void (^blk)(void) = ^{            printf("%c \n",text[2]);        };

The following compilation error occurs:

In fact, the method of intercepting automatic variables in the block does not intercept the C language array. In this case, you can use pointers to solve this problem.

The above code can be changed a little.

const char *text = "hello world";        void (^blk)(void) = ^{            printf("%c \n",text[2]);        };        blk();

The output character is L.


V,

The block keeps a strong pointer pointing to all objects used in the block, that is, these referenced objects are kept in heap until the block lifecycle ends.

Memorycycle:

The following code defines attributes and uses blocks (in the Controller class:

@ Property (nonatomic, strong) nsmutablearray * myblocks; // block array

[Self. myblocksaddobject: ^ {// block can be used as an object

[Selfdosomething];

}];

Because the block references self, both the self (through the myblocks attribute) and the block have a strong pointer pointing to each other. At this time, both the self and the block cannot be released from the memory!

 

Solution:

Worker defines a weak pointer: worker:

_ Weakmyclass * weakobj = self;

// Arrayofblocks

@ Property (nonatomic, strong) nsarray * myblocks;

[Self. myblocks addobject: ^ {

[Weakobjdosomething];

}];

Block has a weak pointer pointing to weakobj, and self has a strong pointer pointing to myblock.

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.