深入理解Block
2019-08-01 本文已影响9人
zwwuchn
什么是block
- 带有自动变量(局部变量)的匿名函数。它是C语言的扩充功能
- block本质上是一个OC对象,它内部也有isa指针
- block是封装了函数调用以及函数调用环境的OC对象
通过Clang编译器了解block结构
void(^block)(void) = ^{
NSLog(@"This is a block");
};
block();
将OC代码转换为C/C++代码
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
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; // block类型
impl.Flags = flags;
impl.FuncPtr = fp; // block内部执行逻辑
Desc = desc; // 一些描述信息
}
};
// NSLog(@"This is a block"); 这段代码
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders_sk_84zmdndx0m3b5lg8frrxkxf00000gn_T_main_deb9d2_mi_0);
}
// 描述信息
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)};
// 把block信息传递到__main_block_impl_0结构体中
void(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
// 通过__main_block_impl_0.impl->FuncPtr�(block内部执行逻辑)
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
调用顺序
- 1.保存block到__main_block_impl_0结构体中
- 2.__main_block_impl_0构造函数进行赋值
- 3.通过__main_block_impl_0中的impl调用FuncPtr
block变量如何捕获
第一种
int age = 10;
void(^block)(void) = ^{
NSLog(@"This is a jack. age is %d",age);
};
age = 20;
block();
//打印结果:
// This is a jack. age is 10
大家可以导出C++文件查看,__main_block_impl_0结构体内部会自动添加 int age 变量
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o main.cpp
block内部捕获变量age的值是10,然后保存在main_block_impl_0结构体中,虽然设置了age = 20,这样做只是修改了外部age的值,并没有修改main_block_impl_0结构体内部中已经保存的age = 10,所以这种局部变量捕获是值传递
第二种
// auto: 自动变量,离开作用域就会销毁
auto int age = 10; 等价 int age = 10;
// 静态局部变量
static int height = 10;
static int height = 10;
void(^block)(void) = ^{
NSLog(@"This is a jack. height is %d",height);
};
height = 20;
block();
// 打印结果:
// This is a jack. height is 20
block内部捕获静态变量height地址指向是10,然后保存在main_block_impl_0结构体中,设置了height = 20,其实是height地址指向是20,这样做改变了height地址,并修改main_block_impl_0结构体内部中已经保存的height地址,所以这种局部变量捕获是指针传递
第三种
int age = 10;
static int height = 10;
int main(int argc, const char * argv[]) {
@autoreleasepool {
void(^block)(void) = ^{
NSLog(@"This is a jack. age is %d. height is %d",age,height);
};
age = 20;
height = 20;
block();
}
return 0;
}
// 打印结果:
This is a jack. age is 20. height is 20
block内部没有捕获,而是直接访问
图片.pngblock类型
void(^block)(void) = ^{
NSLog(@"This is a jack.");
};
NSLog(@"%@ %@ %@ %@",[block class],[[block class] superclass],[[[block class] superclass] superclass],[[[[block class] superclass] superclass] superclass]);
// 打印结果:
// __NSGlobalBlock__ __NSGlobalBlock NSBlock NSObject
从打印也能证明block是一个OC对象
block有3种类型,可以通过调用class方法或者isa指针查看具体类型,最终都是继承自NSBlock类型
-
NSGlobalBlock ( _NSConcreteGlobalBlock
-
NSStackBlock ( _NSConcreteStackBlock )
-
NSMallocBlock ( _NSConcreteMallocBlock )
图片.png
block环境变换
图片.pngblock的copy
图片.png图片.png
__block修饰符
block可以解决block内部无法修改auto变量值的问题
__block int age = 10;
void(^block)(void) = ^{
age = 20;
NSLog(@"This is a jack. age is %d",age);
};
block();
编译器会把__block变量包装成一个对象
struct __Block_byref_age_0 {
void *__isa;
__Block_byref_age_0 *__forwarding;
int __flags;
int __size;
int age;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_age_0 *age; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_age_0 *_age, int flags=0) : age(_age->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
编译后的block代码
__attribute__((__blocks__(byref))) __Block_byref_age_0 age = {(void*)0,(__Block_byref_age_0 *)&age, 0, sizeof(__Block_byref_age_0), 10};
void(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_age_0 *)&age, 570425344));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
图片.png
__block内存管理
__block修饰变量类型
图片.png图片.png
图片.png
图片.png
__block修饰对象类型
struct __Block_byref_person_0 {
void *__isa;
__Block_byref_person_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
LPPerson *person;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_person_0 *person; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_person_0 *_person, int flags=0) : person(_person->__forwarding) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_person_0 *person = __cself->person; // bound by ref
NSLog((NSString *)&__NSConstantStringImpl__var_folders_sk_84zmdndx0m3b5lg8frrxkxf00000gn_T_main_a9918c_mi_0,(person->__forwarding->person));
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->person, (void*)src->person, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->person, 8/*BLOCK_FIELD_IS_BYREF*/);}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};
编译后的block代码
__attribute__((__blocks__(byref))) __Block_byref_person_0 person = {(void*)0,(__Block_byref_person_0 *)&person, 33554432, sizeof(__Block_byref_person_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, ((LPPerson *(*)(id, SEL))(void *)objc_msgSend)((id)((LPPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("LPPerson"), sel_registerName("alloc")), sel_registerName("init"))};
void(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_person_0 *)&person, 570425344));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
图片.png
block解决循环引用
-
ARC
-
用weak、unsafe_unretained解决循环引用
-
__block也可以解决循环引用(对象设置为nil,最后必须要调用block)
-
-
MRC
-
用__unsafe_unretained解决循环引用
-
__block也可以解决循环引用
-