Block 简述
Blocks是C语言的扩充功能,是一种带有自动变量的匿名函数。具体来说就是没有函数名称和能带自动变量。
Blocks的存储域有:NSConcreteStackBlock(在栈区)、NSConcreteGlobalBlock(数据区)、NSConcreteMallocBlock(堆区) ARC情况下栈区声明的Block会自动复制到堆区,这是为什么超过作用域还能存在的理由
看看block实现原理
int main(int argc, const char * argv[]) {
@autoreleasepool {
void(^blk)(int count) = ^void(int count){
NSLog(@"%d",count);
};
blk(10);
}
return 0;
}
用clang -rewrite-objc + 原文件名 可以看到源代码
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;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself, int count) {
NSLog((NSString *)&__NSConstantStringImpl__var_folders__l_qvvd4l511c3bxr5z7px1xgx80000gn_T_XQBlockTest_ad2fef_mi_0,count);
}
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(*blk)(int count) = ((void (*)(int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
((void (*)(__block_impl *, int))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk, 10);
}
return 0;
}
主要有三个结构体
1. struct __block_impl 结构体其形式如下
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
isa用于存储其类对象的信息,给其赋值一个Block的实例,机指向Block的对象的具体实例 跟objective-c 处理运行时很像。FunPtr是指其函数指针,其他信息不太重要
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)};
这个主要包含内存地址和结构体版本信息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;
}
};
这个结构体又有上述两个结构体为其成员变量,而且初始化这个block时给他进行了赋值
Blocks 模式
语法
最详细的block语法模式是: ^ 返回值类型 参数列表 表达式
void(^myBlock)(int count) = ^void(int count){
printf("result is :%d \n",count*2);
};
myBlock(10);
但是block的各种省略模式眼花缭乱,无论怎么省略这个符号是一定不能省略的 ^
Blocks 类型变量
Objective-c中一般用typedef声明Blocks变量
typedef int(^myBlock3)(int count);
myBlock3 blk = ^int (int testCount){
return testCount * 2;
};
printf("testCount is %d \n",blk(10));
Blocks 截获自动变量值
Blocks截获自动变量其实是保存了这个变量的瞬间值,并且blocks中增加了这个成员变量为这个变量,并且为其赋值为当时保存的瞬值,这里我们不能再对它进行赋值操作了,不然会报错的,这里我们可以理解为时截取到类对象的结构体实例指针,显然赋值是会报错的。
int a = 10;
TestBlk testBlk = ^{
NSLog(@"a is %d ",a);
};
testBlk();
其实截获自动变量值就是在在上述的struct __main_block_impl_0的机构体重自增新的的成员变量
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;
}
};
__block 说明符
用block说明符声明的变量可以进行赋值操作
__block int a = 10;
void(^blk5)(void) = ^{
a = 11;
};
printf("a is %d \n",a);
blk5();
打印结果为:
a is 11
__block 修饰符将变量设置在哪个储存域中 看看源码
int main(int argc, const char * argv[]) {
@autoreleasepool {
__block int objc_a = 10;
void(^blk)(void) = ^{
objc_a = 11;
};
NSLog(@"objc_a is %d",objc_a);
blk();
}
return 0;
}
看看clang -rewrite 转换成C++的源码
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __Block_byref_objc_a_0 {
void *__isa;
__Block_byref_objc_a_0 *__forwarding;
int __flags;
int __size;
int objc_a;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_objc_a_0 *objc_a; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_objc_a_0 *_objc_a, int flags=0) : objc_a(_objc_a->__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_objc_a_0 *objc_a = __cself->objc_a; // bound by ref
(objc_a->__forwarding->objc_a) = 11;
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->objc_a, (void*)src->objc_a, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->objc_a, 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};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
__attribute__((__blocks__(byref))) __Block_byref_objc_a_0 objc_a = {(void*)0,(__Block_byref_objc_a_0 *)&objc_a, 0, sizeof(__Block_byref_objc_a_0), 10};
void(*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_objc_a_0 *)&objc_a, 570425344));
NSLog((NSString *)&__NSConstantStringImpl__var_folders__l_qvvd4l511c3bxr5z7px1xgx80000gn_T_XQBlockTest_906fa9_mi_0,(objc_a.__forwarding->objc_a));
((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
}
return 0;
}
这里面比前面block实现的源代码多了一个结构体,而且这个结构体自增到struct __main_block_impl_0 这个结构体中当成员变量,这个新增的结构体神奇的地方就是 它有一个成员变量 __forwarding,而这个__forwarding又是指向该实例自身的指针,然后通过__forwarding访问其成员变量的值,这也是Block超出变量作用域可存在的理由。
struct __Block_byref_objc_a_0 {
void *__isa;
__Block_byref_objc_a_0 *__forwarding;
int __flags;
int __size;
int objc_a;
};
截获的自动变量
我们截获的自动变量,我们能使用它,但不能给他赋值。要给他赋值的话加上 __block修饰符。这里我们能使用它的原因是因为我们截取了NSMutableArray类对象用的结构体实例指针。而且在使用C语言字符串的数组用 * 代替[],不然会报错。
MutableArray *array = [NSMutableArray array];
void(^blk)(void) = ^{
TestClass *class = [[TestClass alloc] init];
[array addObject:class];
};
blk();
NSLog(@"array count is %d",array.count);
未完待续………………