自我会保留在块内吗? [英] Will self retain within block?
本文介绍了自我会保留在块内吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在调用该块之前/之后,保留计数始终为1. 来自苹果阻止文档,我们知道自己应该保留.谁能知道为什么吗?
Before/After call the block, the retaincount is always 1. From apple block doc we know that the self should retain. Can anyone know why?
NSLog(@"Before block retain count: %d", [self retainCount]);
void (^block)(void) = ^(void){
UIImage* img = [UIImage imageNamed:@"hometown.png"];
[self setImage:img];
NSLog(@"After block retain count: %d", [self retainCount]);
};
block();
推荐答案
首先, retainCount没有用.不要叫它..
块仅在复制块时保留捕获的对象.因此,该示例中的块不会保留self
.
Blocks only retain captured objects when the block is copied. Thus, self
won't be retained by the block in that example.
这篇关于自我会保留在块内吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文