Avoiding the “capturing self strongly in this block is likely to lead to a retain cycle” message

后端 未结 3 960
情书的邮戳
情书的邮戳 2021-02-03 11:00

every time I have to use a global var or property inside a block like this:

self.save = ^(){
  if (isItSaving == NO) {
      [self saveMyFile];
  }
};

3条回答
  •  生来不讨喜
    2021-02-03 11:29

    In addition to @NikolaiRuhe's response, in your example when declaring the properties

    BOOL *iis = isItSaving;
    id myself = self;
    

    implies strong references, so use __weak self to prevent the retain cycle. Then you might wonder why you need to declare a __strong reference to weak self within the block, and that's to make sure it doesn't get released during the life of the block, otherwise weakSelf->isItSaving would break if self was released.

提交回复
热议问题