【发布时间】:2017-07-06 03:36:00
【问题描述】:
我们知道我们需要在块内使用弱引用来打破保留循环,如下所示:
__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
有什么区别?计时器如何仍然保留目标?
【问题讨论】:
-
顺便说一句,我们在定义
weakSelf时一般使用typeof(self)而不是id。
标签: ios objective-c objective-c-blocks nstimer