如何调试 Swift 的函数调用堆栈?

How can I debug a Swift's function call stack?

我遇到屏幕被推送两次的问题。问题是我不知道哪个 pushViewController 正在这样做,而且我有很多。所以我想给 navigationController.pushViewController() 函数添加一个断点,这样我就可以看到调用该函数的前一个调用堆栈是什么。我不需要看到 pushViewController 的内部。我只需要在调用 navigationController 上的 pushViewController 函数时断点停止。但是我不能在 navigationController 代码的 open func pushViewController() 上放置断点。我该怎么做?

您可以扩展 UINavigationController 并覆盖 pushViewController 函数。

class MyNavigationViewController : UINavigationController {
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        super.pushViewController(viewController, animated: animated)
    }
}

然后,如果您使用的是故事板,请将导航控制器的 class 更改为 MyNavigationViewController,如果您从代码创建导航控制器,则还要在代码中更改。