ios面试汇总Objective - C 底层

Objective - C block(一)block的底层结构

2020-05-06  本文已影响0人  爱玩游戏的iOS菜鸟

(一)Block的常见问题

首先我们先看下面几个问题:

  1. block的原理是怎样的?本质是什么?

  2. __block的作用是什么?有什么使用注意点?

  3. block的属性修饰词为什么是copy?使用block有哪些使用注意?

  4. block在修改NSMutableArray,需不需要添加__block?

如果上面的不能完整的回答,说明block的知识需要进行深刻的学习了

(二)block的本质及底层结构

下面我们通过多个方法去印证。

(1)block的底层结构

我们看下面的代码:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        ^{
            NSLog(@"hello,world!");
        }();
        
        int age = 10;
        void(^block)(int,int) = ^(int a,int b){
            NSLog(@"this is a block == %d" , age);
            NSLog(@"this is a block a == %d b == %d" , a, b);
        };
        
        block(20,30);
        
        
    }
    return 0;
}

通过clang编译器转化为C++代码后(截取主要代码):

//struct __block_impl结构
struct __block_impl {
  void *isa;//isa指针
  int Flags;
  int Reserved;
  void *FuncPtr;
};

//第一个block的底层结构
struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  //构造函数(C++) 
  __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调用函数
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {

            NSLog((NSString *)&__NSConstantStringImpl__var_folders_f9__q8q2xr131vf4h1cy7837sm00000gn_T_main_26fdd2_mi_0);
        }

//第一个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)};

//第二个block的底层结构(包含变量age)
struct __main_block_impl_1 {
  struct __block_impl impl;
  struct __main_block_desc_1* Desc;
  int age;
  __main_block_impl_1(void *fp, struct __main_block_desc_1 *desc, int _age, int flags=0) : age(_age) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

//第二个block调用函数
static void __main_block_func_1(struct __main_block_impl_1 *__cself, int a, int b) {
  int age = __cself->age; // bound by copy

            NSLog((NSString *)&__NSConstantStringImpl__var_folders_f9__q8q2xr131vf4h1cy7837sm00000gn_T_main_26fdd2_mi_1 , age);
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_f9__q8q2xr131vf4h1cy7837sm00000gn_T_main_26fdd2_mi_2 , a, b);
        }

//第二个block描述信息
static struct __main_block_desc_1 {
  size_t reserved;
  size_t Block_size;
} __main_block_desc_1_DATA = { 0, sizeof(struct __main_block_impl_1)};

int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
        ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA))();
    //简化后 &__main_block_impl_0(__main_block_func_0,&__main_block_desc_0_DATA)

        int age = 10;
        void(*block)(int,int) = ((void (*)(int, int))&__main_block_impl_1((void *)__main_block_func_1, &__main_block_desc_1_DATA, age));
    //简化后  block = &__main_block_impl_1(__main_block_func_1, &__main_block_desc_1_DATA, age)
        ((void (*)(__block_impl *, int, int))((__block_impl *)block)->FuncPtr)((__block_impl *)block, 20, 30);
    //简化后  (block -> FuncPtr)(block, 20, 30)
    }
    return 0;
}

我们通过转化后的代码可以证实。另外,我们通过,类型强转也可以查看block的底层结构


通过类型强转查看底层结构 image.png
总结
block的底层结构图解
(2)block的本质
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        int age = 10;
        void(^block)(int,int) = ^(int a,int b){
            NSLog(@"this is a block == %d" , age);
            NSLog(@"this is a block a == %d b == %d" , a, b);
        };
        block(20,30);
    }
    return 0;
}

通过clang编译器转化为C++代码后(截取主要代码):

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;
  int age;
  //构造函数(类似于init方法)
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _age, int flags=0) : age(_age) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

//将block的内部执行逻辑封装到__main_block_func_0函数
static void __main_block_func_0(struct __main_block_impl_0 *__cself, int a, int b) {
  int age = __cself->age; // bound by copy

            NSLog((NSString *)&__NSConstantStringImpl__var_folders_f9__q8q2xr131vf4h1cy7837sm00000gn_T_main_142399_mi_1 , age);
            NSLog((NSString *)&__NSConstantStringImpl__var_folders_f9__q8q2xr131vf4h1cy7837sm00000gn_T_main_142399_mi_2 , a, b);
        }

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; 
         int age = 10;
        //定义block变量  
        void(*block)(int,int) = ((void (*)(int, int))&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, age));
        //简化:block = &__main_block_impl_0(__main_block_func_0, &__main_block_desc_0_DATA, age) 参数1:函数地址 参数2:block的描述 参数3:age

        //执行block内部代码
        ((void (*)(__block_impl *, int, int))((__block_impl *)block)->FuncPtr)((__block_impl *)block, 20, 30);
        // //简化:(block->FuncPtr)(block, 20, 30) 函数调用
    }
    return 0;
}
(2)block的本质
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int age = 10;
        void(^block)(int,int) = ^(int a,int b){
            NSLog(@"this is a block a == %d" , age);
        };
        block(20,30);
        
        NSLog(@"%@",[block class]);//__NSMallocBlock__
        NSLog(@"%@",[[block class] superclass]);//__NSMallocBlock
        NSLog(@"%@",[[[block class] superclass] superclass]);//NSBlock
        NSLog(@"%@",[[[[block class] superclass] superclass] superclass]);//NSObject
        NSLog(@"%@",[[[[[block class] superclass] superclass] superclass] superclass]);//(null)
    }
    return 0;
}

从输出结果来看,block是一个OC对象,基类是NSObject,下一章我们对block的类型进行进一步的学习。

上一篇下一篇

猜你喜欢

热点阅读