Question one: The life cycle of a block object?
Look directly at the code, what is the print result?
#import <foundation/foundation.h>//defines the Block object (Global block) int (^externblock) (void) = ^ ( void) {return 100;};/ /define function (block as parameter) void function (int (^block) (void)) { printf ("The result of executing the block is: %d\n\n ", block ());} Defined two functions Void firstfunc (Int number) { //define block Objects (local) int (^blockwithinfirst) (void) = ^ (void) { return number; }; //calls the external function function function (blockwithinfirst); Assign a value externblock = blockwithinfirst;} Void secondfunc (int number) { //int Local variables int Index = 10; //block Local Object int (^blockwithinsecond) ( void) = ^ (void) {return number * index;}; //calls an external function function function (Blockwithinsecond);} Int main (int argc, const char * argv[]) { @ AUTORELEASEPOOL&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;//1. Calling function Functions function (Externblock); //2. Call Firstfunc firstfunc (5); / /3. Call Secondfunc secondfunc (Ten); //4. As in step 1 function (ExternBlock); } return 0;}
Question two: How to solve the problem of accessing global variable / local static variable / local variable inblock body ( investigate __block type )
#import <foundation/foundation.h>void function (int number, void (^blockParam) ( void)) { //prints the incoming number value printf ("Incoming number is: %d\n", number); //Call the incoming block object blockparam ();} int globalnumber = 1000;//global variable (external variable) Int main (int argc, const char * argv[]) { @autoreleasepool { //Block declaration (no parameters, no return value) void (^internalblock) (void); //Local static variables static int localstaticint = 20; //local variable:local Variable (free variable: free variable) int localint = 20; //the first part: The parameter of the most function of the block object is called inside the function block //block Definition (prints the values of the above three variables); internalblock = ^ (void) { printf ("Global variable globalnumber:%d; local static variable localstaticint:%d; local variable localint: %d\n ", globalnumber, localstaticint, localint); }; //the Internalblock block object as a function parameter, call function functions function (1, internalblock); //Part Two: Modify the values of the three variables above, Recall function Functions globalNumber = 3000; localStaticInt = 0; localInt = 0; function (2,&NBSP;INTERNALBLOck); //Part III: Re-write the definition of block again, and then call function functions internalblock = ^ (void) { printf ("Global variable globalnumber:%d; Local static variable localstaticint:%d; local variable localint:%d\n ", globalnumber, localstaticint, localint); }; function (3, internalblock); //Part IV: Study on __block type modification __block int localAnotherInt = 20; void (^firstblock) (void) = ^ (void) { globalNumber += 100; localStaticInt += 200; localAnotherInt = 10; printf ("globalnumber:%d;localstaticint:%d; localanotherint:%d\n\n after re-assignment", globalnumber, Localstaticint,localanotherint); }; //Call/Execute Firstblock firstblock (); } return 0;}
Analysis:
Initialize: Global : +; Local static :; Local Variables :
A. function(1, internalblock); The package contains the executable code, and also contains the values of the accessible variables ( Global : +; local static: ; Local Variables :)
B. globalnumber = +;
Localstaticint = 0 ;
Localint = 0 ;
function(2, internalblock);
package contains executable code and also contains values for accessible variables ( global :; local static : 0; local variables : ); block Does the body have no modified permissions on the local variable, or the data of the package that was originally executed for the first time
C. redefine internalblock ( global : +; local static : 0; local variables : 0 )
internalblock = ^ (void) { printf(" global variable globalnumber:%d; local static variable localstaticint:%d; Local Variables localint:%d\n " , Globalnumber , Localstaticint, Localint); };
function(3, internalblock);
d. If you add the type of the local variable __block type that can be Block modifies the value of the local variable in the body ( after re-assigning the value of the globalnumber:3100;localstaticint:200; Localanotherint:10 )
question three: Block body cycle application problem, how to solve (study __weak type)
Trblockobject *blockobj = [Trblockobject new]; Blockobj.block = ^void (void) {NSLog (@ "blockobj%@", blockobj); };
In the example, the Block property is the Blockobj object, and in the block body, the block references the Blockobj object, causing a circular reference.
The solution is to:
Trblockobject *blockobj = [Trblockobject new]; Scenarios that resolve circular references use the __weak modifier. __weak trblockobject *weakblockobj = blockobj; Blockobj.block = ^void (void) {NSLog (@ "blockobj%@", weakblockobj); };
__weak is a weak reference, when Blockobj was released, Weakblockobj is already nil.
Block should be aware of three questions!