美文网首页
唯一标示符&Block原理

唯一标示符&Block原理

作者: honzon_0 | 来源:发表于2017-04-28 18:53 被阅读22次

    唯一标示符符

    NSUUID

    系统并没有存储

    NSString *uuid = [[NSUUID UUID] UUIDString];
    NSLog(@"%@", uuid);
    //-> C7933B11-B8EE-49A8-A628-C285AEBDBC24
    

    广告标示符(IDFA-identifierForIdentifier)

    广告标示符是由系统存储着的

    NSString *adUDID = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    NSLog(@"%@",adUDID);
    //-> D3B19AFD-A6B6-4551-8CF0-1070A3D1F756
    

    Vindor标示符 (IDFV-identifierForVendor)vendor

    非常简单:一个Vendor是CFBundleIdentifier(反转DNS格式)的前两部分。例如,com.doubleencore.app1 和 com.doubleencore.app2 得到的identifierForVendor是相同的, 如果用户卸载了同一个vendor对应的所有程序,然后在重新安装同一个vendor提供的程序,此时identifierForVendor会被重置。

    NSString *idfv = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    NSLog(@"%@",idfv);
    //-> B18BD9D8-D815-4774-813F-7135471BCFC7
    

    iOS唯一标示符引导

    Block

    void (^ block)(void) = ^ {
        printf("honzon");
    };
    block();
    

    经过clang -rewrite-objc main.mmain.m转成main.cpp文件

    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;
      __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("honzon");
     }
     
    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)};
    
    //声明
    void (* block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA));
    //执行
    ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
    
    

    __main_block_impl 结构体 对应一个block的对象,包含一个block的所有信息

    __main_block_func 函数实现 具体的block需要执行的代码

    __main_block_desc 内存管理

    __block_impl 函数管理

    引用外部变量

    int integer_2 = 2;
    void (^block_2)(void) = ^ {
        printf("integer %d", integer_2);
    };
    block_2();
    
    struct __main_block_impl_1 {
      struct __block_impl impl;
      struct __main_block_desc_1* Desc;
      int integer_2;
      __main_block_impl_1(void *fp, struct __main_block_desc_1 *desc, int _integer_2, int flags=0) : integer_2(_integer_2) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    static void __main_block_func_1(struct __main_block_impl_1 *__cself) {
      int integer_2 = __cself->integer_2; // bound by copy
    
                printf("integer %d", integer_2);
        }
    
    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)};
    
    

    可以看到,引用外部变量的block,最开始在__main_block_impl_1中添加了一个成员变量integer_2,然后在构造函数中对该变量进行赋值, 最后在__main_block_func_1中取出该值使用。需要注意的是,在使用之前,进行了int integer_2 = __cself->integer_2;操作,来创建一个临时变量以进行后续操作。因为是值传递,所以并不会改变外部变量。

    使用__Block修饰的外部变量

    __block int integer_3 = 3;
    void (^block_3)(void) = ^ {
        printf("integer %d", integer_3);
    };
    block_3();
    
    struct __Block_byref_integer_3_0 {
      void *__isa;
    __Block_byref_integer_3_0 *__forwarding;
     int __flags;
     int __size;
     int integer_3;
    };
    
    
    struct __main_block_impl_2 {
      struct __block_impl impl;
      struct __main_block_desc_2* Desc;
      __Block_byref_integer_3_0 *integer_3; // by ref
      __main_block_impl_2(void *fp, struct __main_block_desc_2 *desc, __Block_byref_integer_3_0 *_integer_3, int flags=0) : integer_3(_integer_3->__forwarding) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    static void __main_block_func_2(struct __main_block_impl_2 *__cself) {
      __Block_byref_integer_3_0 *integer_3 = __cself->integer_3; // bound by ref
    
                printf("integer %d", (integer_3->__forwarding->integer_3));
            }
    static void __main_block_copy_2(struct __main_block_impl_2*dst, struct __main_block_impl_2*src) {_Block_object_assign((void*)&dst->integer_3, (void*)src->integer_3, 8/*BLOCK_FIELD_IS_BYREF*/);}
    
    static void __main_block_dispose_2(struct __main_block_impl_2*src) {_Block_object_dispose((void*)src->integer_3, 8/*BLOCK_FIELD_IS_BYREF*/);}
    
    static struct __main_block_desc_2 {
      size_t reserved;
      size_t Block_size;
      void (*copy)(struct __main_block_impl_2*, struct __main_block_impl_2*);
      void (*dispose)(struct __main_block_impl_2*);
    } __main_block_desc_2_DATA = { 0, sizeof(struct __main_block_impl_2), __main_block_copy_2, __main_block_dispose_2};
    
    
    __attribute__((__blocks__(byref))) __Block_byref_integer_3_0 integer_3 = {(void*)0,(__Block_byref_integer_3_0 *)&integer_3, 0, sizeof(__Block_byref_integer_3_0), 3};
    
    void (*block_3)(void) = ((void (*)())&__main_block_impl_2((void *)__main_block_func_2, &__main_block_desc_2_DATA, (__Block_byref_integer_3_0 *)&integer_3, 570425344));
    
    ((void (*)(__block_impl *))((__block_impl *)block_3)->FuncPtr)((__block_impl *)block_3);
    

    大的改动相比不添加__block修饰并不多,主要是增加一个新的结构体__Block_byref_integer_3_0来保存变量的信息(主要是地址),然后声明block_3的时候,将__block修饰的变量的地址传进去,然后保存在__Block_byref_integer_3_0中,最后在__main_block_func_2中通过地址去操作变量。

    如果截获的自动变量是OC的对象,在ARC下,将会强引用这个对象一次从而保证了原对象不被销毁,但与此同时,也会导致循环引用问题.

    而根据isa指针,block一共有3种类型的

    block_NSConcreteGlobalBlock 全局静态

    _NSConcreteStackBlock保存在栈中,出函数作用域就销毁

    _NSConcreteMallocBlock 保存在堆中,retainCount == 0销毁

    你真的理解__block修饰符的原理么?

    Joshua Shen的回答

    相关文章

      网友评论

          本文标题:唯一标示符&Block原理

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