对象捕捉
- block对象捕捉也称为capture,下面我用代码来解释一下
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSInteger age = 10;
void (^block)(void) = ^{
NSLog(@"age is %zd",age);
};
age = 20;
block();
}
return 0;
}
这里先看看结果,大家心中的结果是输入的10,还是20呢?
age is 10 //结果是10
我们来分析一下底层文件,先转换为clang
cd 目录
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m
//这样我们就拿到了main.cpp文件
我们来分析一下main.cpp
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger age;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger _age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
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;
NSInteger age = 10;//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;
}
如果没有c++基础,或者c语言基础的人看这个有点不大理解,我将这段代码转换为oc,删除类型转换再看看
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger age;
//构造函数
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger _age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
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;
NSInteger age = 10;
void (*block)(void) = &__main_block_impl_0(
__main_block_func_0,
&__main_block_desc_0_DATA, age);
age = 20;
//调用
block->FuncPtr(block);
}
return 0;
}
这样看是不是清晰很多 创建的方法里面内部我上一篇文章已经讲到了,调用是不是也很明朗,那么我们来看看他内部的对象捕捉是怎么捕捉的
&__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA, age)) 传递了 __main_block_func_0结构体 __main_block_desc_0_DATA结构体
age对象__main_block_impl_0 里面有个age属性
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger age; //内部age
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger _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) {
NSInteger age = __cself->age; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_v2_sw9667rd2fn8s8hvf6pqn3ph0000gn_T_main_dca09e_mi_0,age);
}
- __main_block_impl_0 里面的age属性接受外部创建时候传递的age值
- __main_block_func_0 通过当前self获取age的值(这里保存的是值 而不是内存地址)
- NSLog((NSString *)&__NSConstantStringImpl__var_folders_v2_sw9667rd2fn8s8hvf6pqn3ph0000gn_T_main_dca09e_mi_0,age); 所以这里输出的是10
重点来了 那么block对象捕捉究竟是怎么回事
- auto:自动变量,离开作用域就销毁(默认就是)
- 加入static 就输出20,这是为什么呢(直接上代码)
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
static NSInteger age = 10;
void (^block)(void) = ^{
NSLog(@"age is %zd",age);
};
age = 20;
block();
}
return 0;
}
//输出
2018-07-31 15:58:23.963515+0800 block[18242:299786] age is 20
Program ended with exit code: 0
1.为啥加入static就会输出20呢?
2.我们来看看
用的auto 输出10
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger *age;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger *_age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
用的static 输出20
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger *age;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger *_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) {
NSInteger *age = __cself->age; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_v2_sw9667rd2fn8s8hvf6pqn3ph0000gn_T_main_ac64eb_mi_0,(*age));
}
很明显 当用static的时候 传递进去的是*_age 也就是说他是保存的age的内存地址,然后通过他的内存地址取出里面的age属性 __cself->age
当auto的时候 里面保存的是age的值 也就是当进入到block(),其实他的内存地址早已经销毁了,所以你更改他外部的值,里面并没有什么改变。
还有一种情况 就是全局变量,全局变量会不会捕捉呢,答案是不会,直接上代码
#import
int height_ = 20;
int main(int argc, const char * argv[]) {
@autoreleasepool {
static NSInteger age = 10;
void (^block)(void) = ^{
NSLog(@"age is %zd,height =%d",age,height_);
};
age = 20;
block();
}
return 0;
}
//新增了一个height_ 全局属性
//输出
2018-07-31 16:08:43.333358+0800 block[18297:305751] age is 20,height =20
Program ended with exit code: 0
直接来看看c++代码
#pragma clang assume_nonnull end
int height_ = 20;
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
NSInteger *age;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger *_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) {
NSInteger *age = __cself->age; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_v2_sw9667rd2fn8s8hvf6pqn3ph0000gn_T_main_8d76a9_mi_0,(*age),height_);
}
- 我们可以看到他里面有一个height_ 全局属性,并没有生成height_属性,里面也没有传递height_属性,所以他是不会对象捕捉的