gpt4 book ai didi

ios - block 中自身的引用计数

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:24:26 24 4
gpt4 key购买 nike

我正在努力了解对象生命周期和引用计数如何与代码块交互。在下面的代码中,我只是做一个简单的动画,当 UINavigationController 的堆栈上的顶 View 被交换时闪烁。棘手的部分是弹出的 View Controller 是定义此代码的地方

[UIView animateWithDuration:0.2 
animations:^{self.navigationController.view.alpha = 0.0;}
completion:^(BOOL finished){
UINavigationController *navController = self.navigationController;

[self.navigationController popViewControllerAnimated:NO];
[navController pushViewController:nextView animated:NO];
[nextView release];

[UIView animateWithDuration:0.2
animations:^{navController.view.alpha = 1.0;}];
}];

我的问题是(忽略动画的样子),从内存管理的角度来看,这是执行此操作的正确方法吗?特别是:

(1) 将这种方法用于 pop+push 循环时,不再需要保留 self 是否正确,如 other similar examples不使用 block ?

(2) 使用这些 block 调用 animateWithDuration:... 是否保留定义 View Controller (自身)直到 block 执行?

最佳答案

(1) When using this approach the the pop+push cycle, is it correct that it is no longer necessary to retain self, as in other similar examples that do not use blocks?

这是正确的。如果 nextView 是局部变量,这些 block 会自动保留 self、navController 和 nextView。

(2) Does invoking animateWithDuration:... with the these blocks retain the defining view controller (self) until the blocks execute?

这些 block 通过这种方法从栈中复制到堆中。这些 block 在执行后被释放。然后从这些 block 中释放 self、navController 和 nextView。

关于ios - block 中自身的引用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5395743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com