美文网首页
IOS Blocks详解-Block的实质

IOS Blocks详解-Block的实质

作者: copy_farmer | 来源:发表于2022-01-09 21:31 被阅读0次

    1.Block 实质

    通过clang(LLVM编译器)我们可以把block转换为我们想要的可读代码,该代码是c++源码,但实际上也就用了struct的结构,本质为C语言

    clang -rewrite-objc  main.m   //无uikit库调用
    clang -x objective-c -rewrite-objc -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk main.m //含uikit库调用
    

    为了进一步弄清Block在底层的代码逻辑,我们先将函数简化如下并运行上面的命令得到main.cpp文件

    void (^blk)(void) = ^{
            printf("Block function");
          };
        blk();
    
    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("Block function");
          }
    
    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, char * argv[]) {
    
        void (*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
        ((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
    
        return 0;
    }
    

    与C++的this和OC的self相同,__cself是__main_block_impl_0的结构体指针,该结构体有两个成员变量 一个是impl 一个是Desc指针,便于理解我们先将如下代码简化

    void (*blk)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
    
    struct __main_block_impl_0 tmp = __main_block_impl_0(__main_block_func_0,&__main_block_desc_0_DATA);
    struct __main_block_impl_0 *blk = &tmp;
    
    

    不难看出,将Block语法生成的Block赋给Block类型变量blk,等同于将__main_block_impl_0的结构体实例的指针赋值给变量blk,
    再来看blk部分

    ((void (*)(__block_impl *))((__block_impl *)blk)->FuncPtr)((__block_impl *)blk);
    
    (*blk->impl.FuncPtr)(blk);
    

    这就是简单的使用函数指针调用函数,由代码看出Block真是作为参数进行了传递

    关于impl 中impl.isa = &_NSConcreteStackBlock; _NSConcreteStackBlock可以看做class_t的结构体实例,将Block作为OC对象处理时,该类信息放在_NSConcreteStackBlock中,isa 对象指向类 对象的isa指向本类 类的isa指向元类 元类的isa指向根元类 根元类的isa指向自己

    相关文章

      网友评论

          本文标题:IOS Blocks详解-Block的实质

          本文链接:https://www.haomeiwen.com/subject/gexycrtx.html