是否有可能在没有__weak对象的块中传递[self anyFunction]?
作为示例,这是来自System Framework的有效代码:
[UIView animateWithDuration:0.8 animations:^{
//Do animationStuff
} completion:^(BOOL finished) {
[self anyFunction];
}];
Run Code Online (Sandbox Code Playgroud)
您可以[self anyFunction]在没有警告的情况下传入完成块.但是如果使用完成块编写自己的方法,则会出现以下警告:在此块中强烈捕获"self"可能会导致保留周期.
工作解决方案非常简单(iOS 5 + ARC).在块声明之前:
__weak MyClass *weakSelf = self;
Run Code Online (Sandbox Code Playgroud)
在完成块中你必须打电话:
[weakSelf anyFunction];
Run Code Online (Sandbox Code Playgroud)
但是,回到我的问题:为什么System Framework API中不需要使用__weak对象并在self没有任何警告的情况下使用.以及如何__weak在块中不需要对象的情况下实现方法?
感谢你的付出.
iphone objective-c ios objective-c-blocks automatic-ref-counting