可以在没有 __weak 对象的块中传递 [self anyFunction] (iOS 5 + ARC)
是否可以在没有来自 self 的 __weak 对象的情况下在块中传递 [self anyFunction] ?
作为示例,这是来自系统框架的有效代码:
[UIView animateWithDuration:0.8 animations:^{
//Do animationStuff
} completion:^(BOOL finished) {
[self anyFunction];
}];
您可以在完成块中传递 [self anyFunction]
而不会发出警告。但是,如果您使用完成块编写自己的方法,则会出现以下警告:在此块中强烈捕获“self”可能会导致保留周期。
可行的解决方案非常简单(iOS 5 + ARC)。在块声明之前:
__weak MyClass *weakSelf = self;
在完成块中,您必须调用:
[weakSelf anyFunction];
但是,回到我的问题: 为什么系统框架 API 中不需要使用 __weak
对象并使用 self
没有任何警告。以及如何在块中不需要 __weak
对象的情况下实现方法?
感谢您的努力。
Is it possible to pass [self anyFunction] in blocks without a __weak object from self?
As an example this is valid code from the System Framework:
[UIView animateWithDuration:0.8 animations:^{
//Do animationStuff
} completion:^(BOOL finished) {
[self anyFunction];
}];
You can pass [self anyFunction]
in the completion block without a warning. But if you write your own method with a completion block, the following warning occurs: capturing 'self' strongly in this block is likely to lead to a retain cycle.
A working solution is quite simple (iOS 5 + ARC). Before the block declare:
__weak MyClass *weakSelf = self;
and in the completion block you have to call:
[weakSelf anyFunction];
But, back to my Question: Why there is no need in the System Framework APIs to use a __weak
object and to use self
without any warnings. And how to implement a method without the need of a __weak
object in the block?
Thank you for your effort.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引发错误的块是您捕获拥有该块的对象的块。例如
或
但是
因为一个对象保留它的块,而一个块保留它的对象。因此,在这两种情况下,执行该块的对象拥有该块,该块也拥有该对象。所以你有一个循环 - 一个保留循环。这意味着内存泄漏了。
在您给出的示例中 - 您正在查看类方法。您正在调用
UIView
类上的块,而不是UIView
对象。类没有与其关联的内存。并且您可能从控制器调用此函数,因此 self 引用被块保留,但没有循环,因为 self 没有保留块。同样,您可能已经注意到,并非块中使用的所有对象都需要弱引用 - 只是那些导致保留循环的对象。
The blocks which throw up the error are ones where you capture the objects that own the block. For example
or
but
Because an object retains its blocks, and a block retains it's objects. So in both these cases, the object which performs the block owns the block, which also owns the object. So you have a loop - a retain cycle. which means the memory is leaked.
In the example you have given - you're looking at a class method. You're calling the block on a
UIView
class, not aUIView
object. A class has no memory associated with it. And you are probably calling this function from a controller, so theself
reference is being retained by the block, but there is no loop because self is not retaining the block.In the same way that, you may have noticed, not all objects that are used in the block need to be weakly referenced - just the ones that cause a retain cycle.
在我需要使用或不使用 ARC 或使用或不使用较新的编译器进行潜在编译的代码上,我执行以下操作...功能上它与您已经列出的相同,但它避免了 __weak 并且也避免了保留释放周期:
On code that I need to compile potentially with or without ARC, or with or without the newer compilers, I do the following ... functionally it's the same as what you've listed already, but it avoids the__weak and also avoids the retain release cycles: