1.普通自动变量(局部变量)
NSString *string = @"123";
void (^block)(void) = ^{
NSLog(@"----%@",string);
};
block编译后的结构体中会自动新增类型名称相同的成员变量,即(NSString *string;)此时block外部的string与block内部的string实则为两个独立的变量,改变block外部的string并不会影响到block内部string的值,改变block内部string则会产生编译错误
2.静态变量、静态全局变量、全局变量
对于静态全局变量、全局变量,由于作用域是全局的,所以直接访问就好。
对于静态变量
static int number = 123;
static NSString *string = @"123";
void (^block)(void) = ^{
NSLog(@"--%d--%@",number,string);
};
block编译后会将number和string的指针地址(&number,&string)存储至自身的结构体中作为成员变量,即(int *number,NSString **string;),这样在block内外修改变量则是同步的(block内部通过指针间接修改,外部直接修改)
3.__block修饰的变量
__block int number = 1;
void (^block)(void) = ^{
number = 2;
};
被__block修饰的变量在编译后会转变成结构体对象,如下:
struct __Block_byref_number_0 {
void *__isa;
__Block_byref_number_0 *__forwarding;
int __flags;
int __size;
int number;
};
__block int number = 1;编译后为:
__Block_byref_number_0 number = {(void*)0,(__Block_byref_number_0 *)&number, 0, sizeof(__Block_byref_number_0), 1};
可看到初始化的 number结构体下的__forwarding存储的是number结构体实例自己的地址
block编译后的结构体中也会新增一个成员变量(__Block_byref_number_0 *number;)初始化block的时候会将block外部的number的成员变量__forwarding赋值给block结构体内的number,(block ->number = number->__forwarding),因此block内部新增的是一个指向外部number结构体的指针
对number进行修改时,实则会修改__forwarding指向的结构体下的number的值
number = 2; 编译后如下:
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_number_0 *number = __cself->number; // bound by ref
(number->__forwarding->number) = 2;
}
此处的__forwarding存储的是外部number结构体的地址,外部number结构体下的__forwarding存储的是它自身的地址,因此,不管修改block内部还是外部的number,实则都是修改同一个__Block_byref_number_0结构体下的number
当block被从栈上复制到堆上时,__block修饰的number同样会被从栈上复制到堆上,此时则存在两个number结构体,一个在栈上一个在堆上,block内部访问__block变量时 则会访问堆上的__block变量,在block外部修改number时,则访问的是栈上的number结构体,但在将栈上的__block变量复制到堆上时,会将它的成员变量__forwarding的值替换为复制目标堆上的__block变量结构体实例的地址,因此修改栈上的number结构体实例时,实际是通过__forwarding修改堆上的number结构体下的成员变量number的值
附编译后的代码
int main(int argc, char * argv[]) {
__block int number = 1;
void (^block)(void) = ^{
number = 2;
};
}
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __Block_byref_number_0 {
void *__isa;
__Block_byref_number_0 *__forwarding;
int __flags;
int __size;
int number;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
__Block_byref_number_0 *number; // by ref
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_number_0 *_number, int flags=0) : number(_number->__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_number_0 *number = __cself->number; // bound by ref
(number->__forwarding->number) = 2;
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->number, (void*)src->number, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->number, 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, char * argv[]) {
__attribute__((__blocks__(byref))) __Block_byref_number_0 number = {(void*)0,(__Block_byref_number_0 *)&number, 0, sizeof(__Block_byref_number_0), 1};
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, (__Block_byref_number_0 *)&number, 570425344));
}