Static variables
In the previous article, we learned how to use block global variables. Static variables, like global variables, can be directly used inside the block or modified inside the block.
Reference official documents:
Global variables are accessible, including static variables that exist within the enclosing lexical scope.
Let's look at a piece of code:
Declare a static variable and modify it within the block
Static nsstring * _ para1;-(void) test4 {_ para1 = @ "para1"; // Initial Value nslog (@ "init para1: % @, % P, % P ", _ para1, & _ para1, _ para1); void (^ myblock) (INT) = ^ (INT num) {// assign a value in the block to _ para1 = @ "para3 "; nslog (@ "excuteing para1: % @, % P, % P", _ para1, & _ para1, _ para1 );}; // value _ para1 = @ "para2"; nslog (@ "excutebefore para1: % @, % P, % P", _ para1, & _ para1, _ para1); myblock (1); // nslog (@ "excuteafter para1: % @, % P, % P", _ para1, & _ para1, _ para1 );}
Output:
2014-07-28 17:05:47.701 Test[2307:60b] init para1:para1,0x39bb0,0x399e42014-07-28 17:05:47.704 Test[2307:60b] excutebefore para1:para2,0x39bb0,0x39a242014-07-28 17:05:47.705 Test[2307:60b] excuteing para1:para3,0x39bb0,0x39a042014-07-28 17:05:47.706 Test[2307:60b] excuteafter para1:para3,0x39bb0,0x39a04
From the log, we can see that the variable address in the block is the same as the external address, and the static variable can be modified.
Let's take a look at the converted code:
static NSString * _para1;struct __KDBlockTest__test4_block_impl_0 { struct __block_impl impl; struct __KDBlockTest__test4_block_desc_0* Desc; __KDBlockTest__test4_block_impl_0(void *fp, struct __KDBlockTest__test4_block_desc_0 *desc, int flags=0) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; }};
Because the static object exists in the static zone (Global zone) from creation to program destruction, the corresponding members are not declared in the block. The following is our test4 function:
static void _I_KDBlockTest_test4(KDBlockTest * self, SEL _cmd) { _para1=(NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_1; NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_2,_para1,&_para1,_para1); void (*myBlock)(int) = (void (*)(int))&__KDBlockTest__test4_block_impl_0((void *)__KDBlockTest__test4_block_func_0, &__KDBlockTest__test4_block_desc_0_DATA); _para1=(NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_5; NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_6,_para1,&_para1,_para1); ((void (*)(__block_impl *, int))((__block_impl *)myBlock)->FuncPtr)((__block_impl *)myBlock, 1); NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_7,_para1,&_para1,_para1);}
Block initialization, passed in the pointer and description function to implement the function, and there are no other parameters.
void (*myBlock)(int) = (void (*)(int))&__KDBlockTest__test4_block_impl_0((void *)__KDBlockTest__test4_block_func_0, &__KDBlockTest__test4_block_desc_0_DATA);
Let's take a look at the implemented functions and directly reference static variables.
static void __KDBlockTest__test4_block_func_0(struct __KDBlockTest__test4_block_impl_0 *__cself, int num) { _para1=(NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_3; NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_2l25j3tn0wl_3zy1hpsq1rhc0000gp_T_KDBlockTest_3979e1_mi_4,_para1,&_para1,_para1); }
On the basis of circular reference, static variables and member variables use the same method, but the principle is still different.