不,没关系,该块保留s
. 所以s
当你打电话时不会被释放[s release]
。
更多信息:如果创建保留周期,您可能会遇到问题。大多数情况下,它看起来像这样:
[self doSomeThingWithCompletionHandler:^()
{
[self beHappy];
}
自我确实保留了块,块保留了自我,瞧。但它不必如此明显:
NSDictionary *dictionary = @{@"someKey" : someObj, @"someOtherKey", someOtherObj};
//
// lots of stuff happening here
//
[someObj doSomethingWithCompletionHandler:^()
{
id someOtherObj = dictionary[@"someOtherKey"]
}]
解决方案是声明一个弱变量:
__weak id weakSelf = self;
[self doSomethingWithCompletionHandler:^()
{
//and to make sure it stays valid if you do multithreaded stuff
__strong id strongSelf = weakSelf;
[strongSelf beHappy];
}];
编辑:正如评论中指出的那样,原始帖子使用的是非 ARC 代码。在这种情况下,您必须替换__weak
为__unsafe_unretained