ARC并阻止混乱

时间:2015-05-21 21:27:55

标签: ios objective-c automatic-ref-counting

我在OBJ-C中讨论了一些关于ARC的文章(https://dhoerl.wordpress.com/2013/04/23/i-finally-figured-out-weakself-and-strongself/http://tech.pro/tutorial/1227/blocks-gcd-and-pitfalls-to-avoidAlways pass weak reference of self into block in ARC?),但这些事情对我来说仍然不清楚:

- (NSArray *)rightButtons{
    __typeof(self) __weak weakSelf = self;
    JAActionButton *button1 = [JAActionButton actionButtonWithTitle:@"Archive" color:kArchiveButtonColor handler:^(UIButton *actionButton, JASwipeCell*cell) {
        [cell completePinToTopViewAnimation];
        [weakSelf rightMostButtonSwipeCompleted:cell];
    }];

    return @[button1];
}

button1位于rightButtons方法的词法范围内。它不是这个类的财产。为什么我需要将weakSelf传递给块呢?编译器是否将button1视为self.button1(那么在块中使用weakSelf会有意义吗?)自我如何保留这个块以便你需要weakSelf?

1 个答案:

答案 0 :(得分:0)

Self可能没有直接引用button1,但self可能引用了其他引用button1的内容。

像: 自我 - >查看 - > button1 - >自