有两种办法,
第一种是可以修改 static 全局变量;
第二种是可以修改用新关键字 __block 修饰的变量。请看:
void (^aBlock)(void) = ^(void){
NSLog(@" Sum: %d\n", global + staticLocal);
global++;
blockLocal++;
staticLocal++;
};
aBlock();
NSLog(@"After modified, global: %d, block local: %d, static local: %d\n", global, blockLocal, staticLocal);
After modified, global: 11, block local: 11, static local: 11
第一种是可以修改 static 全局变量;
第二种是可以修改用新关键字 __block 修饰的变量。请看:
static int global = 10;
__block int blockLocal = 10;
void (^aBlock)(void) = ^(void){
NSLog(@" Sum: %d\n", global + staticLocal);
global++;
blockLocal++;
staticLocal++;
};
aBlock();
NSLog(@"After modified, global: %d, block local: %d, static local: %d\n", global, blockLocal, staticLocal);
After modified, global: 11, block local: 11, static local: 11