search

Home  >  Q&A  >  body text

objective-c - 关于Masonry为什么不会造成循环引用的问题

代码举个例子,我不知道自己的理解对不对。

[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.otherView.mas_centerY);
}];

block中持有了self,但是self.view并没有持有这个block,因为看到Masonry的源码是这样的:

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    block(constraintMaker);
    return [constraintMaker install];
}

它仅仅是block(constrainMaker)。如果改成了self.block = block(constrainMaker),那么view是不是也持有了block呢

高洛峰高洛峰2757 days ago650

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-02 09:32:40

    It is not true that block will definitely cause a circular reference. Whether it is a circular reference depends on whether they hold strong references to each other. If self is used in a block, the block will keep a reference to self, but self does not hold the block directly or indirectly, so it will not cause a circular reference.

    Your understanding is correct.

    reply
    0
  • Cancelreply