objective-c - Question about why Masonry does not cause circular references
高洛峰
高洛峰 2017-05-02 09:30:40
0
1
677

The code is an example. I don’t know if my understanding is correct.

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

The block holds self, but self.view does not hold this block, because the source code of Masonry is as follows:

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

It is just block(constrainMaker). If it is changed to self.block = block(constrainMaker), does the view also hold the block?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(1)
我想大声告诉你

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template