当我在协议扩展中实现的方法中创建UIStoryBoard对象时,得到EXC_BAD_ACCESS

时间:2018-12-11 20:28:55

标签: swift xcode uiviewcontroller uistoryboard exc-bad-access

我在项目中使用https://github.com/agordeev/OverlayViewController中的OverlayViewController代码。

此代码中有一个协议和一个扩展,它们为协议的实现添加了两种方法:

protocol OverlayHost {
    func showOverlay<T: OverlayViewController>(type: T.Type, storyboardName: String) -> T?
    func showOverlay<T: OverlayViewController>(identifier: String, storyboardName: String) -> T?
}

extension OverlayHost where Self: UIViewController {

    @discardableResult
    func showOverlay<T: OverlayViewController>(type: T.Type, fromStoryboardWithName storyboardName: String) -> T? {
        let identifier = String(describing: T.self)
        return showOverlay(identifier: identifier, fromStoryboardWithName: storyboardName)
    }

    @discardableResult
    func showOverlay<T: OverlayViewController>(identifier: String, fromStoryboardWithName storyboardName: String) -> T? {
        let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
        guard let overlay = storyboard.instantiateViewController(withIdentifier: identifier) as? T else { return nil }
        overlay.presentOverlay(from: self)
        return overlay
    }
}

当我尝试呈现叠加层(情节提要中的UIViewController)时,什么都没有发生。在探索中,我注意到当上面的代码创建UIStoryboard对象时,出现错误:

let storyboard = UIStoryboard(name: storyboardName, bundle: nil)

产生错误:

expression produced error: error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x5a10432956f).
The process has been returned to the state before expression evaluation.

如果我拨打扩展名外的那一行,则看不到此错误。

另一点可能相关或可能不相关的是,发生错误的扩展名中的方法的调用来自父级扩展名中的方法:

extension MenuViewController: UITableViewDelegate {

...

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 2 {
            showOverlay(type: OverlayViewController.self, storyboardName: "MenuStoryboard")
        }
    }
}

想法?

更新:因此,该叠加层有效,但是我仍然在Xcode中看到此错误。

0 个答案:

没有答案