为了能够保证block正常访问外部的变量,block有个变量捕获机制,如下图
auto:自动变量,平时我们定义int age = 10
,前面有个auto,auto int age = 10
,系统帮我们默认的加上了一个auto。-----值传递
-----
static:静态变量 -----指针传递
-----
全局变量 -----直接访问
-----
根据上面结论,我们一个个展开讨论和分析。
一、自动变量auto修饰的变量,auto int age = 10;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
auto int age = 10;
void (^block)(void) = ^() {
NSLog(@"age = %d,",age);
};
age = 20;
block();
}
return 0;
}
此main函数中的block,是我们常见的OC代码,那我们从声明 int age = 10;
到将age的值改变age = 20
,最后block输出的age会等于20吗
带着这个疑问,我们来分析一下底层代码的实现,OC-->C++的转换过程:cd到程序main.m的目录下,执行命令xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m
,可以得到一个main.cpp文件,拖到工程目录中。
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int age;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int age = __cself->age; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_0xn052bn6dgb9z7pfk8bbg740000gn_T_main_6a0dd1_mi_0,age);
}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
int age = 10;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, age));
age = 20;
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
return 0;
}
这堆代码,大家都很熟悉了吧,从上一章节讲block-底层数据结构就分析过的,看看定义了int age = 10
的区别
从上图中的源码中,我们可以留意到外部声明的
int age = 10
在block里使用做的事情为了能够保证block正常访问外部的变量,block有个变量捕获机制,那什么类型会捕获到block内部,什么类型不会捕获呢,我们来探讨一下
- 1.在main函数中,在block定义时,block将外部的age当做参数传给了
__main_block_impl_0
- 2.
__main_block_impl_0
已经将外面声明age捕捉赋值到block内部,既block内部也定义了一个int age;
类型 - 3.block把main函数定义的
int age = 10
的值当做参数赋值给了block里面的age - 4.此时改变age的值,只是改变main函数里的age的值,无法改变block内部的age的值,两个是属于不同函数体内的age,互不相关。
- 5.
block()
调用时,输出的age = __cselef->age
,就是等于block内部(__main_block_impl_0
)的age值
auto变量总结,由上面的分析,我们可验证自动变量auto修饰的变量,是有捕捉到block内部,并且是属于值传递。
二、静态变量static修饰的变量,static int age = 10;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
static int age = 10;
void (^block)(void) = ^() {
NSLog(@"age = %d,",age);
};
age = 20;
block();
}
return 0;
}
// 控制台的输出
2018-06-13 15:30:13.674073+0800 block-变量的捕获[92711:10064124] age = 20,
Program ended with exit code: 0
static修饰的变量age,打印的时候age = 20,为什么用static就能修改了呢?我们来看一下下面的转换成C++后的代码,分析一下底层的实现。
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int *age;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int *_age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int *age = __cself->age; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_0xn052bn6dgb9z7pfk8bbg740000gn_T_main_73b8c0_mi_0,(*age));
}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
static int age = 10;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, &age));
age = 20;
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
return 0;
}
static变量总结,由上图的分析,我们可以得知,static 修饰的age,是会捕获进block内部,并且捕获的是age的地址,所以外面age变成20的时候,block内部输出的age也是等于20,因为外部和block内部的age指向的是同一地址。
三、全局变量
int age = 10;
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
void (^block)(void) = ^() {
NSLog(@"age = %d,",age);
};
age = 20;
block();
}
return 0;
}
// 控制台的打印
2018-06-13 16:08:54.737079+0800 block-变量的捕获[96358:10119188] age = 20,
Program ended with exit code: 0
当age时全局变量的时候,打印的结果为20,按常理来分析,全部变量的作用域是默认的情况下是所有的函数
,生命周期是程序结束时
,所以block里打印等于20,应该大家都能理解,那我们来看一下block内部有没有捕获到age呢,分析一下源码,如下
int age = 10;
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_0xn052bn6dgb9z7pfk8bbg740000gn_T_main_164d03_mi_0,age);
}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
age = 20;
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
return 0;
}