美文网首页
解决block循环引用--ARC

解决block循环引用--ARC

作者: 大鹅ne | 来源:发表于2020-04-17 17:53 被阅读0次

    🌟用__weak,__unsafe_unretained解决

        __weak typeof(self) weakSelf = self;
        self.block = ^{
            __strong typeof(weakSelf) myself = weakSelf;
            
            NSLog(@"age is %d", myself->_age);
        };
    
    
        __unsafe_unretained typeof(self) weakSelf = self;
        self.block = ^{
            NSLog(@"age is %d", weakSelf.age);
        };
    
    
    
    
    Screen Shot 2020-04-17 at 5.51.27 PM.png

    🌟用__block解决(必须要调用block)

    __block id weakSelf = self;
    self.block = ^{
         printf("%p",weakSelf);
         weakSelf = nil;
    };
    self.block();
    
    Screen Shot 2020-04-17 at 5.53.20 PM.png

    相关文章

      网友评论

          本文标题:解决block循环引用--ARC

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