一、Block简介
带有自动变量的匿名函数,实质就是Objective-C的对象。
1、Block语法
^ returnType paramType expression
^ int (int count) {return count++;}
^ (int count) {return count++;}
^ void (void) {printf("Hello Block");}
^ {printf("Hello Block");}
2、Block声明
// 函数指针
int (*funcptr) (int)
// Block声明
int (^block) (int)
// Block类型作为函数参数
void func (int (^block) (int))
// Block类型作为属性
@property (nonatomic, copy) void(^block)(void);
// typedef
typedef int (^block) (int)
二、Block实质
1、转换源代码
// 转换源代码指令
clang -rewrite-objc [fileName]
xcrun -sdk iphoneos clang -rewrite-objc ViewController.m
xcrun -sdk iphonesimulator clang -rewrite-objc ViewController.m
源代码
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
void (^JDPBlock) (void) = ^{printf("Hello, Block!");};
JDPBlock();
}
return 0;
}
转换
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) {
printf("Hello, Block!");}
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 (*JDPBlock) (void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
((void (*)(__block_impl *))((__block_impl *)JDPBlock)->FuncPtr)((__block_impl *)JDPBlock);
}
return 0;
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
可理解为
struct A
struct B, init b
function f(x)
struct X {
A,
B,
constructor(F,B, Flag)
}
x = X(f, b)
x.f(x)
2、Block截获自动变量
源代码
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSInteger integer = 1;
void (^JDPBlock1) (void) = ^{printf("%lu\n", integer);};
integer = 2;
JDPBlock1();
}
return 0;
}
转换
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;
NSInteger integer;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSInteger _integer, int flags=0) : integer(_integer) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
NSInteger integer = __cself->integer; // bound by copy
printf("%d", integer);}
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 integer = 1;
void (*JDPBlock1) (void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, integer));
integer = 2;
((void (*)(__block_impl *))((__block_impl *)JDPBlock1)->FuncPtr)((__block_impl *)JDPBlock1);
}
return 0;
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
理解为
struct A
struct B, init b
function f(x) {
i = x.i
}
struct X {
A,
B,
I,
constructor(F, B, I, Flag)
}
i = 1
x = X(f, b, i)
i = 2;
x.f(x)
1、Block语法表达式中使用的自动变量被作为成员变量被添加到结构体中。
2、Block语法表达式中没有使用的自动变量不会被追加。
3、Block截获自动变量值,意味着在定义Block时,Block语法表达式所使用的自动变量值被保存到Block的结构体实例中。
4、Block定义后,自动变量值的修改不会影响到Block内的成员变量的值。
3、__block变量
源代码
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
__block NSInteger integer = 1;
void (^JDPBlock1) (void) = ^{
integer = 3;
printf("%lu\n", integer);
};
integer = 2;
JDPBlock1();
}
return 0;
}
3
转换
struct __Block_byref_integer_0 {
void *__isa;
__Block_byref_integer_0 *__forwarding;
int __flags;
int __size;
NSInteger integer;
};
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;
__Block_byref_integer_0 *integer; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_integer_0 *_integer, int flags=0) : integer(_integer->__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_integer_0 *integer = __cself->integer; // bound by ref
(integer->__forwarding->integer) = 3;
printf("%lu\n", (integer->__forwarding->integer));
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->integer, (void*)src->integer, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->integer, 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_integer_0 integer = {(void*)0,(__Block_byref_integer_0 *)&integer, 0, sizeof(__Block_byref_integer_0), 1};
void (*JDPBlock1) (void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_integer_0 *)&integer, 570425344));
(integer.__forwarding->integer) = 2;
((void (*)(__block_impl *))((__block_impl *)JDPBlock1)->FuncPtr)((__block_impl *)JDPBlock1);
}
return 0;
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
理解为
struct Q
struct A
struct B, init b
function f(x) {
q = x.q
q.i = 3
}
struct X {
A,
B,
Q,
constructor(F, B, Q, flag)
}
q = constructor(0, &q, 0, size(Q), 1)
x = X(f, b, q)
q.i = 2;
x.f(x)
三、Block类型
1、Block和__block变量的实质是栈上生成的结构体实例。
2、Block的类型有_NSConcreteGlobalBlock、_NSConcreteStackBlock、_NSConcreteMallocBlock,分别在数据区、栈和堆中。
3、Block不截获自动变量时,就可以将Block设置在程序的数据区中。
4、__block结构体,成员变量__forwarding可以实现无论__block变量配置在栈还是堆上时都能够正确地访问同一个__block变量。
5、ARC有效时,编译器自动将Block从栈复制到堆,何时自动?
5.1、调用Block的copy实例方法时
5.2、Block作为函数返回值返回时
5.3、将Block赋值给附有__strong修饰符的变量时
5.4、在方法名中含有usingBlock的Cocoa框架方法或者GCD的API中传递Block时
6、向方法或函数的参数中传递Block时,编译器不能判断是否自动拷贝,Cocoa usingBlock和GCD除外。
int main(int argc, const char * argv[]) {
@autoreleasepool {
void (^JDPBlock) (void) = ^{printf("Hello, Block!");};
NSLog(@"%@", JDPBlock);
}
return 0;
}
<__NSGlobalBlock__: 0x100001050>
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSInteger i = 1;
void (^JDPBlock) (void) = ^{printf("%lu", i);};
NSLog(@"%@", JDPBlock);
}
return 0;
}
// MRC
<__NSStackBlock__: 0x7ffeefbff588>
// ARC
<__NSMallocBlock__: 0x1005969b0>
四、Block&对象
1、Block截获对象
源代码
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *array = [[NSMutableArray alloc] init];
void (^JDPBlock) (void) = ^{NSLog(@"%@", array);};
JDPBlock();
}
return 0;
}
转换
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;
NSMutableArray *array;
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, NSMutableArray *_array, int flags=0) : array(_array) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
NSMutableArray *array = __cself->array; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_yp_b0w71dps58s5rncj_t1znym80000gn_T_main_6313b8_mi_0, array);}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->array, (void*)src->array, 3/*BLOCK_FIELD_IS_OBJECT*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->array, 3/*BLOCK_FIELD_IS_OBJECT*/);}
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;
NSMutableArray *array = ((NSMutableArray *(*)(id, SEL))(void *)objc_msgSend)((id)((NSMutableArray *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSMutableArray"), sel_registerName("alloc")), sel_registerName("init"));
void (*JDPBlock) (void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, array, 570425344));
((void (*)(__block_impl *))((__block_impl *)JDPBlock)->FuncPtr)((__block_impl *)JDPBlock);
}
return 0;
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
理解为
struct A
struct B, init b
function f(x) {
NSMutableArray *array = x.array;
NSLog(@"%@", array);
}
struct X {
A,
B,
NSMutableArray *array;
constructor(F, B, NSMutableArray *array, Flag)
}
NSMutableArray *array = [[NSMutableArray alloc] init];
x = X(f, b, array)
x.f(x)
2、__block修饰对象
源代码
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
__block NSMutableArray *array = [[NSMutableArray alloc] init];
void (^JDPBlock) (void) = ^{NSLog(@"%@", array);};
JDPBlock();
}
return 0;
}
转换
struct __Block_byref_array_0 {
void *__isa;
__Block_byref_array_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
NSMutableArray *array;
};
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;
__Block_byref_array_0 *array; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_array_0 *_array, int flags=0) : array(_array->__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_array_0 *array = __cself->array; // bound by ref
NSLog((NSString *)&__NSConstantStringImpl__var_folders_yp_b0w71dps58s5rncj_t1znym80000gn_T_main_b13376_mi_0, (array->__forwarding->array));}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->array, (void*)src->array, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->array, 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_array_0 array = {(void*)0,(__Block_byref_array_0 *)&array, 33554432, sizeof(__Block_byref_array_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, ((NSMutableArray *(*)(id, SEL))(void *)objc_msgSend)((id)((NSMutableArray *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSMutableArray"), sel_registerName("alloc")), sel_registerName("init"))};
void (*JDPBlock) (void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_array_0 *)&array, 570425344));
((void (*)(__block_impl *))((__block_impl *)JDPBlock)->FuncPtr)((__block_impl *)JDPBlock);
}
return 0;
}
static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
理解为
struct Q
struct A
struct B, init b
function f(x) {
q = x.q
NSLog(@"%@", x.array);
}
struct X {
A,
B,
Q;
constructor(F, B, Q, Flag)
}
NSMutableArray *array = [[NSMutableArray alloc] init];
q = Q(0, &q, 0, sizeof(Q), array);
x = X(f, b, q)
x.f(x)
五、Block循环引用
MRC中,__block修饰符被用来避免Block中的循环引用。这是由于当Block从栈复制到堆时,若Block使用的变量为附有__block修饰符的id类型或对象类型的自动变量,不会被retain。若Block使用的变量为没有__block修饰符的id类型或对象类型的自动变量,则被retain。