我们知道,为了打破引用循环,我们需要在块内使用弱引用,像这样:
__weak id weakSelf = self;
[self doSomethingWithABlock:^() {
[weakSelf doAnotherThing];
}]
然而,弱引用无法打破由NSTimer
引起的保留循环。
__weak id weakSelf = self;
timer = [NSTimer scheduledTimerWithTimeInterval:30.0f
target:weakSelf
selector:@selector(tick)
userInfo:nil
repeats:YES]; // No luck
什么是区别?计时器如何仍保留目标?