在扩展中定义的递归函数中访问self时的EXC_BAD_ACCESS

时间:2018-03-24 15:24:28

标签: ios swift uikit

在扩展程序中定义的递归函数中访问self时,我遇到意外的EXC_BAD_ACCESS错误。

我已将设置的复杂性降低到一个简单的(ish)示例,该示例仍然展示了该问题。我有一个ThemeableViewController协议和一个协议扩展,它定义了一个函数实现(cascaseInitialise(withTheme)):

protocol ThemeableViewController where Self: UIViewController { }

extension ThemeableViewController {
    func cascadeInitialise(withTheme theme: Theme) {

        // will perform other initialisation here; excluded for testing

        if let tabVC = self as? UITabBarController, let innerVCs = tabVC.viewControllers {
            for themableVC in innerVCs.flatMap({$0 as? ThemeableViewController}) {
                themableVC.cascadeInitialise(withTheme: theme)
            }
        }

    }
}

我使用了扩展来使其他UIViewController类符合此协议:

extension UITabBarController: ThemeableViewController { }

extension UISplitViewController: ThemeableViewController { }

我的根视图控制器是UITabBarController;它的4个视图控制器都是UISplitViewController个。我在我的根视图控制器上调用cascadeInitialise,如下所示:

let rootTabBarVC = UIApplication.shared.delegate!.window!!.rootViewController as! UITabBarController
rootTabBarVC.cascadeInitialise(withTheme: .normal)

cascadeInitialise第二次调用中(即当selfUISplitViewController时),我发现if let tabVC行上的EXC_BAD_ACCESS崩溃}。请参阅以下堆栈跟踪:

奇怪的是,在调试时,如果我在该行上放置一个断点,在控制台中输入po self as? UITabBarController not 会导致错误(它会按预期返回{{ 1}}):

Xcode debugger; crash

我无法解决这里的问题。

0 个答案:

没有答案
相关问题