美文网首页
2020-03-09 block里面的typeof 对self造

2020-03-09 block里面的typeof 对self造

作者: 我是小胡胡分胡 | 来源:发表于2020-03-09 23:21 被阅读0次

    1、一般写法

            __weak typeof(self) weakSelf = self;
    
            __strong typeof(self) strongSelf = weakSelf;
    
    

    2、Rac做法

    2.1, @unsafeify(self); @strongify(self);

     @autoreleasepool {} __attribute__((objc_ownership(none))) __typeof__(self) self_weak_ = (self);;
        {
            @autoreleasepool {}
    
    #pragma clang diagnostic push
    
    #pragma clang diagnostic ignored "-Wshadow"
    
     __attribute__((objc_ownership(strong))) __typeof__(self) self = self_weak_;
    
    #pragma clang diagnostic pop
    
    ;
    
        }
    
    

    2.2, @weakify(self); @strongify(self);

     @autoreleasepool {} __attribute__((objc_ownership(weak))) __typeof__(self) self_weak_ = (self);;
        {
            @autoreleasepool {}
    
    
    #pragma clang diagnostic push
    
    #pragma clang diagnostic ignored "-Wshadow"
    
     __attribute__((objc_ownership(strong))) __typeof__(self) self = self_weak_;
    
    #pragma clang diagnostic pop
    
    ;
            self.title;
        }
    
    

    3、YYKit做法

    3.1 @weakify(self);@strongify(self);

        @autoreleasepool{} __attribute__((objc_ownership(weak))) __typeof__(self) weak_self = self;;
    
        @autoreleasepool{} __typeof__(self) self = weak_self;;
    
    

    注意关键词的使用 objc_ownership

    4、总结一下语法格式

    @autoreleasepool {}
    typeof(self) __attribute__((objc_ownership(none)))     unsafeself
    typeof(self) __attribute__((objc_ownership(weak)))   weakself
    typeof(self) __attribute__((objc_ownership(strong)))  strongself
    __attribute__((objc_ownership(weak))) __typeof__(self)    weakself
    __typeof__(self) strongself 
    

    问题:
    放在block内的 ,无论是typeof(self)还是 __typeof__(self)这两种写法,此处的self 不会造成强引用吗?

    相关文章

      网友评论

          本文标题:2020-03-09 block里面的typeof 对self造

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