因爲我們知道,我們需要使用一個弱引用塊內部打破保留週期,就像這樣:差異
__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
有什麼區別?定時器如何仍能保持目標?
BTW,定義'weakSelf'時我們通常使用'typeof(self)'而不是'id'。 – Rob