gpt4 book ai didi

swift - 关闭 ViewController OS X (Swift) 时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-28 12:43:02 24 4
gpt4 key购买 nike

这应该是 OS X Swift 的决定

问题 - 应用程序在代表 NSViewController

时崩溃

有两个 NSViewController - 问题是,当我在它们之间进行转换时 - 应用程序关闭并在表示 NSViewControllers 时出现错误。
其中使用了 presentViewController 和 dismissViewController 的方法。

此处有问题的示例项目:git@github.com:IgorCova/Freelance.git

如何重现错误(仅限优胜美地):
1.我先打开NSViewController
2. 在我打开第二个 NSViewController
3. 接下来,回到第一个(dismiss second)
4. 然后再次尝试打开第二个 NSViewController - 最后,应用程序崩溃了(但在 El Capitan 上一切正常,没有错误)

最佳答案

在您的 SecondViewController 的 viewDidLoad() 中,您将自己注册为“dismisSecondViewController”通知的观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(SecondViewController.dismisSelf),
name:"dismisSecondViewController",
object: nil)

问题是,在 10.11 之前,您必须在释放(即从内存中删除)之前取消注册任何观察者,否则 NSNotificationCenter 将继续向现在占用该释放内存区域的对象发送消息— 它可能是垃圾或其他不响应这些消息的对象。由于您的 View Controller 在您切换它们时显然会被释放,这是您的问题。

因此修复很简单:将以下 deinit 方法添加到您的 SecondViewController

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

关于swift - 关闭 ViewController OS X (Swift) 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961631/

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