引用嵌套块内的弱自我

编程入门 行业动态 更新时间:2024-10-11 13:24:01
本文介绍了引用嵌套块内的弱自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我已经使用

__weak typeof(self) weakSelf = self; [self doABlockOperation:^{ ... }];

在该区块内,如果我嵌套另一个区块:

Inside that block, if I nest another block:

[weakSelf doAnotherBlockOperation:^{ [weakSelf doSomething]; }

会创建一个保留周期吗?我是否需要为weakSelf创建另一个弱引用?

will it create a retain cycle? Do I need to create another weak reference to the weakSelf?

__weak typeof(self) weakerSelf = weakSelf; [weakSelf doAnotherBlockOperation:^{ [weakerSelf doSomething]; }

推荐答案

取决于。

如果实际存储块,则只创建一个保留周期(因为 self 指向块,而块指向自)。如果你不打算存储任何一个块,使用对 self 的强引用就足够了---块将在执行后首先释放,然后它将释放它指向 self 的指针。

You only create a retain cycle if you actually store the block (because self points to the block, and block points to self). If you don't intend to store either of the blocks, using the strong reference to self is good enough --- block will be released first after it got executed, and then it will release it's pointer to self.

在您的特定示例中,除非您执行的操作更多没有显示,你不需要创建任何弱者WevenWeakerSelfs。

In your particular example, unless you're performing more operations which are not shown, you don't need to create any weakerWeakerEvenWeakerSelfs.

更多推荐

引用嵌套块内的弱自我

本文发布于:2023-11-08 06:39:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   自我

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!