gpt4 book ai didi

ios - 弱引用被清零,但对象尚未被释放

转载 作者:行者123 更新时间:2023-11-30 13:05:12 25 4
gpt4 key购买 nike

我有一个 weak 实例变量,它保存在 UINavigationController.viewControllers 堆栈中的 View Controller 上。

我的变量自动变为nil,但 View Controller 尚未被释放(因为UINavigationController拥有它)。

为什么我的弱引用被归零?

class NavController: SuperNavigationController
{
weak var weakViewController: UIViewController?

required override init() {

let rootViewController: UIViewController

if (/* whatever */) {
rootViewController = ViewController1(/*whatever*/)
weakViewController = rootViewController
} else {
/* whatever */
}

/*** `weakViewController` is not `nil` at this point ***/

/***
*** This superclass function just does:
*** super.init(navBarClass:toolbarClass:)
*** viewControllers = [rootViewController]
***/
super.init(rootViewController: rootViewController)
}

// Without this, I get an "unimplemented initializer" exception
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}

...
}

但是一旦我到达viewDidLoadweakViewController就是nil,尽管self.viewControllers.first > 仍然是我初始化时所拥有的完全相同的对象。

UINavigationController 拥有其 viewControllers 的方式有什么奇怪的吗?

编辑:我设法在浅层次上识别并修复原因(请参阅下面的答案),但我仍然想知道为什么会发生这种情况。我很乐意接受并投票支持一个可以解释发生了什么的答案!

最佳答案

弱引用表示,如果没有其他内容指向此,我就不需要它。因此,如果所有者是唯一拥有裁判且较弱的人,则 arc 可以自由地释放它。

https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html

关于ios - 弱引用被清零,但对象尚未被释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39479303/

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