在这个块中强烈捕获自我可能会导致一个保留循环 [英] capturing self strongly in this block is likely to lead to a retain cycle
本文介绍了在这个块中强烈捕获自我可能会导致一个保留循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 xcode 中避免此警告.这是代码片段:
How can I avoid this warning in xcode. Here is the code snippet:
[player(AVPlayer object) addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 100)
queue:nil usingBlock:^(CMTime time) {
current+=1;
if(current==60)
{
min+=(current/60);
current = 0;
}
[timerDisp(UILabel) setText:[NSString stringWithFormat:@"%02d:%02d",min,current]];///warning occurs in this line
}];
推荐答案
这里对 self
的捕获是随着你对 self.timerDisp
的隐式属性访问而来的——你不能从 self
强烈保留的块中引用 self
或 self
上的属性.
The capture of self
here is coming in with your implicit property access of self.timerDisp
- you can't refer to self
or properties on self
from within a block that will be strongly retained by self
.
您可以通过在访问块内的 timerDisp
之前创建对 self
的弱引用来解决此问题:
You can get around this by creating a weak reference to self
before accessing timerDisp
inside your block:
__weak typeof(self) weakSelf = self;
[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, 100)
queue:nil
usingBlock:^(CMTime time) {
current+=1;
if(current==60)
{
min+=(current/60);
current = 0;
}
[weakSelf.timerDisp setText:[NSString stringWithFormat:@"%02d:%02d",min,current]];
}];
这篇关于在这个块中强烈捕获自我可能会导致一个保留循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文