Block 之 Block引用局部变量

前言

这一篇主要着重对__block关键字的理解

例子

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int a = 10;
        void (^helloBlock)(void) = ^(){
            printf("%d\n",a);
        };
        a = 11;
        helloBlock();

    }
    return 0;
}

上面那个例子运行后的结果是10。我们可以编译看一下block里面匿名函数的实现:

1
2
3
4
5
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  int a = __cself->a; // bound by copy

            printf("%d\n",a);
}

bound by copy这里的注释表示,block对它引用的局部变量做了只读拷贝,也就是说block引用的是局部变量的副本。所以在执行block语法后,即使改写block中使用的局部变量的值也不会影响block执行时局部变量的值。
那怎么样才能修改a的值呢?答案就是__block

__block

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        __block int a = 10;
        void (^helloBlock)(void) = ^(){
            printf("%d\n",a);
        };
        a = 11;
        helloBlock();

    }
    return 0;
}

引入__block关键字后,运行结果是11,我们再编译看看block里面匿名函数的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
  __Block_byref_a_0 *a = __cself->a; // bound by ref

            printf("%d\n",(a->__forwarding->a));
}

struct __Block_byref_a_0 {
  void *__isa;
__Block_byref_a_0 *__forwarding;
 int __flags;
 int __size;
 int a;
};

我们看到局部变量a变成一个构造体对象,而不再是一个整形变量,结构体里包含它原来的整形变量。bound by ref这个注释也表明此时已变成引用传递

小小总结

block可以引用局部变量,但是不能修改它,不然就是编译错误,如果想修改它,可以加上__block关键字。但是可以改变全局变量、静态变量、全局静态变量。

Comments