block的三种类型:
-
全局 __ NSGlobalBlock __
void (^block)(void) = ^{ NSLog(@"block"); }; NSLog(@"%@", block);
打印结果:<NSGlobalBlock: 0x100e8c090>
-
堆 __ NSMallocBlock __
捕获外界变量
int a = 10; void (^block)(void) = ^{ NSLog(@"block -- %d", a); }; NSLog(@"%@", block);
打印结果:<NSMallocBlock: 0x281591ce0>
-
栈 __ NSStackBlock __
int a = 10; NSLog(@"%@", ^{ NSLog(@"block -- %d", a); });
打印结果:<NSStackBlock: 0x16bb21c28>
以上打印结果是在ARC模式下输出的
从%@
可以看出block是个object对象
从堆和栈的两段代码看,堆代码里的=
操作符相当于是对block进行了copy
block在使用上很方便,可以保存代码块,但用的时候如果不注意会造成循环引用,看下循环引用的情况 self ->block ->self
@interface ViewController ()
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) void(^block)(void);
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.block = ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.name = @"zy";
});
};
self.block();
}
解决办法1:
__weak typeof(self) weakSelf = self;
self.block = ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
strongSelf.name = @"zy";
NSLog(@"%@", strongSelf.name);
});
};
self.block();
解决方法2:
__block ViewController *vc = self;
self.block = ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
vc.name = @"zy";
NSLog(@"%@", vc.name);
vc = nil;
});
};
self.block();//注意:如果不调用self.block()方法,vc永远不会置nil,内存泄露会一直在
解决方法3:将self当做block的形参传入,这样block就不持有self
@property (nonatomic, copy) void(^block)(ViewController *);
- (void)viewDidLoad {
[super viewDidLoad];
self.block = ^(ViewController *vc){
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
vc.name = @"zy";
NSLog(@"%@", vc.name);
});
};
self.block(self);
}
block内部修改局部变量为什么一定要加__block?
//block.c文件
#include <stdio.h>
int main(){
int a = 10;
void(^block)(void) = ^{
printf("hello %i", a);
};
block();
return 0;
}
查看block.c内部实现
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int a;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
//main函数
int main(){
int a = 10;
void(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
return 0;
}
重点关注函数__main_block_impl_0,struct内部有个属性a,是block.c文件中的block的外部变量a。
修改block.c去掉变量a
//block.c文件
#include <stdio.h>
int main(){
void(^block)(void) = ^{
printf("hello");
};
block();
return 0;
}
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;
}
};
//main函数
int main(){
void(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
return 0;
}
发现函数__main_block_impl_0少传了一个变量a,struct内部也没有属性a。
--》block特性:截获自动变量(局部变量)值,存储在block的结构体内部
回到有变量a的block.c文件,查看对应的block.cpp文件,发现block内执行的操作是对a进行了值传递
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
int a = __cself->a; // bound by copy -- 生成临时变量a = 10 - 地址不一样,值传递
printf("hello %d", a);
}
所以在block.c文件中,block里面的a并不是外部的变量a,且外部a存放在栈上,block在堆空间,堆操作栈不合理,block只能访问不能修改局部变量的值。如果需要改变a需要添加__block标记
//block.c文件
#include <stdio.h>
int main(){
//int a = 10;//栈上
__block int a = 10;
//堆block
void(^block)(void) = ^{
printf("hello %d", a);
a++;
};
block();
return 0;
}
对应block.cpp中,main函数中可以看出,结构体a存放了__block修饰的变量a的值和引用地址,说明block是通过复制a的引用地址来实现访问的
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_a_0 *a = __cself->a; // bound by ref 指针传递
printf("hello %d", (a->__forwarding->a));
(a->__forwarding->a)++;
}
//main函数
int main(){
//结构体
__attribute__((__blocks__(byref))) __Block_byref_a_0 a = {
(void*)0,(__Block_byref_a_0 *)&a,//属性 & 指针
0,
sizeof(__Block_byref_a_0),
10
};
void(*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_a_0 *)&a, 570425344));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
return 0;
}
- block是一个对象
- 能够捕获外界变量,在结构体中生成相应的属性
- 必须使用block()才能执行 - 开始只是申明了一个匿名函数,函数要调用才能执行
- block不能直接操作外界捕获的变量,对外界变量是值拷贝
- __block修饰的外界变量可以进行修改,自动生成结构体存放外界变量的值和指针地址,存放在堆区,堆操作堆,操作性更加合理
推荐学习:runtime源码中block模块,涉及void指针,desc,flag状态等