iOS面试总结

Block解析

2019-06-26  本文已影响0人  Jimmy_L_Wang

什么是Block

Block截获变量

//滴滴面试
{
    static int multiplier = 6;
    int(^Block)(int) = ^int(int num)
    {
        return num * multiplier;
    };
    multiplier = 4;
    NSLog(@"result is %d",Block(2));//result is 12
}

关于Block的截获特性你是否有了解,以及Block的截获特性又是怎样的?

变量的截获方式

#import "MCBlock.h"

@implementation MCBlock
//全局变量
int global_var = 4;
//静态全局变量
static int static_global_var = 5;

- (void)method
{
    //基本数据类型的局部变量
    int var = 1;
    
    //对象类型的局部变量
    __unsafe_unretained id unsafe_obj = nil;
    __strong id strong_obj = nil;
    
    static int static_var = 3;
    
    void(^Block)(void) = ^{
        NSLog(@"局部变量<基本数据类型> var %d", var);
        NSLog(@"局部变量<__unsafe_unretained 对象类型> var %@", unsafe_obj);
        NSLog(@"局部变量<__strong 对象类型> var %@", strong_obj);
        
        NSLog(@"静态变量 %d", static_var);
        NSLog(@"全局变量 %d", global_var);
        NSLog(@"静态全局变量 %d", static_global_var);
    };
    
    Block();
}

@end

clang -rewrite-objc -fobjc-arc MCBlock.m

// @implementation MCBlock

int global_var = 4;

static int static_global_var = 5;


struct __MCBlock__method_block_impl_0 {
  struct __block_impl impl;
  struct __MCBlock__method_block_desc_0* Desc;
//截获局部变量的值
  int var;
  //连同所有权修饰符一起截获
  __unsafe_unretained id unsafe_obj;
  __strong id strong_obj;
  //以指针形式截获局部变量
  int *static_var;
  //对全局变量、静态全局变量不截获
  
  __MCBlock__method_block_impl_0(void *fp, struct __MCBlock__method_block_desc_0 *desc, int _var, __unsafe_unretained id _unsafe_obj, __strong id _strong_obj, int *_static_var, int flags=0) : var(_var), unsafe_obj(_unsafe_obj), strong_obj(_strong_obj), static_var(_static_var) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};
static void __MCBlock__method_block_func_0(struct __MCBlock__method_block_impl_0 *__cself) {
  int var = __cself->var; // bound by copy
  __unsafe_unretained id unsafe_obj = __cself->unsafe_obj; // bound by copy
  __strong id strong_obj = __cself->strong_obj; // bound by copy
  int *static_var = __cself->static_var; // bound by copy

        NSLog((NSString *)&__NSConstantStringImpl__var_folders_hk_552cymzd23v5b0db528k_jbr0000gn_T_MCBlock_a2d5da_mi_0, var);
        NSLog((NSString *)&__NSConstantStringImpl__var_folders_hk_552cymzd23v5b0db528k_jbr0000gn_T_MCBlock_a2d5da_mi_1, unsafe_obj);
        NSLog((NSString *)&__NSConstantStringImpl__var_folders_hk_552cymzd23v5b0db528k_jbr0000gn_T_MCBlock_a2d5da_mi_2, strong_obj);

        NSLog((NSString *)&__NSConstantStringImpl__var_folders_hk_552cymzd23v5b0db528k_jbr0000gn_T_MCBlock_a2d5da_mi_3, (*static_var));
        NSLog((NSString *)&__NSConstantStringImpl__var_folders_hk_552cymzd23v5b0db528k_jbr0000gn_T_MCBlock_a2d5da_mi_4, global_var);
        NSLog((NSString *)&__NSConstantStringImpl__var_folders_hk_552cymzd23v5b0db528k_jbr0000gn_T_MCBlock_a2d5da_mi_5, static_global_var);
    }
static void __MCBlock__method_block_copy_0(struct __MCBlock__method_block_impl_0*dst, struct __MCBlock__method_block_impl_0*src) {_Block_object_assign((void*)&dst->unsafe_obj, (void*)src->unsafe_obj, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->strong_obj, (void*)src->strong_obj, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static void __MCBlock__method_block_dispose_0(struct __MCBlock__method_block_impl_0*src) {_Block_object_dispose((void*)src->unsafe_obj, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->strong_obj, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static struct __MCBlock__method_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __MCBlock__method_block_impl_0*, struct __MCBlock__method_block_impl_0*);
  void (*dispose)(struct __MCBlock__method_block_impl_0*);
} __MCBlock__method_block_desc_0_DATA = { 0, sizeof(struct __MCBlock__method_block_impl_0), __MCBlock__method_block_copy_0, __MCBlock__method_block_dispose_0};

static void _I_MCBlock_method(MCBlock * self, SEL _cmd) {

    int var = 1;


    __attribute__((objc_ownership(none))) id unsafe_obj = __null;
    __attribute__((objc_ownership(strong))) id strong_obj = __null;

    static int static_var = 3;

    void(*Block)(void) = ((void (*)())&__MCBlock__method_block_impl_0((void *)__MCBlock__method_block_func_0, &__MCBlock__method_block_desc_0_DATA, var, unsafe_obj, strong_obj, &static_var, 570425344));

    ((void (*)(__block_impl *))((__block_impl *)Block)->FuncPtr)((__block_impl *)Block);
}

// @end

__block修饰符

    {
        NSMutableArray *array = [NSMutableArray array];
        void(^Block)(void) = ^{
            [array addObject:@123];
        };
        Block();
    }

上面的array是否需要__block修饰?

上面只是对array进行了使用,而不是赋值操作,只有赋值操作才需要

__block修饰的变量变成了对象

{
    __block int multiplier = 6;
    int(^Block)(int) = ^int(int num)
    {
        return num * multiplier;
    };
    multiplier = 4;
    Block(2); //8
}
__block修饰.png 栈区__block.png

所以上面的代码multiplier = 4;实际上变成了(multiplier._forwarding->multiplier) = 4;

Block的类型

Block位置.png

Block的Copy操作

假如声明一个对象的成员变量是Block,而在栈上去创建Block,同时赋值给成员变量的Block,如果成员变量的Block没有使用copy关键字的话,当我们具体通过成员变量去访问对应的Block,可能就由于栈上的Block被释放而引起崩溃。

栈上Block的销毁

block的销毁.png

栈上Block的copy

栈上Block的copy.png

在MRC环境,假如对栈上Block进行copy操作,Block会引起内存泄漏,因为Block copy之后,堆上Block没有额外的成员变量去指向它,就像对一个对象进行alloc操作之后,没有调用对应的release的效果是一样的,产生内存泄漏。

栈上Block的copy02.png

栈上Block进行copy之后,__forwarding指针是指向堆上的__block变量,堆上的Block__forwarding指针指向自身。

__forwarding总结

//百度面试题
{
        __block int multiplier = 10;
        _blk = ^int(int num) {
            return num * multiplier;
        };
        multiplier = 6;
        [self executeBlock];
}
- (void)executeBlock
{
    int result = _blk(4);
    NSLog(@"result is %d", result); //result is 24
}

__forwarding存在的意义

不论在任何内存位置,都可以顺利的访问同一个__block变量。

Block的引用循环

__block的引用循环

{
   __block MCBlock *blockSelf = self;
        _blk = ^int(int num) {
          //var = 2
            return num * blockSelf.var;
        };
        _blk(3);
}
__block的循环引用.png
{
   __block MCBlock *blockSelf = self;
   _blk = ^int(int num) {
          //var = 2
          int result = num * blockSelf.var;
          blockSelf = nil;
          return result;
    };
    _blk(3);//如果Block长时间等不到调用,则引用循环会存在。
}
上一篇 下一篇

猜你喜欢

热点阅读