无法将类型'()'的返回表达式转换为类型'T'

时间:2019-12-10 09:25:34

标签: swift

需要将用于实例化的视图控制器的扩展包装在调度主线程中,但是遇到了该错误,如何解决该错误的任何想法?

enter image description here

extension UIStoryboard {

    convenience init(name: StoryboardName) {
        self.init(name: name.rawValue, bundle: nil)
    }

    func instantiateVC<T: UIViewController>(identifier: String = T.identifier) -> T {
        // swiftlint:disable force_cast
        DispatchQueue.main.async {
            let controller = self.instantiateViewController(withIdentifier: identifier) as! T
            controller.removeBackButtonTitle()
            return controller
        }
        // swiftlint:enable force_cast
    }

    func instantiateInitialVC() -> UIViewController {
        return self.instantiateInitialViewController()!
    }
}

2 个答案:

答案 0 :(得分:1)

尝试仅删除主线程上的按钮标题是否足够

func instantiateVC<T: UIViewController>(identifier: String = T.Identifier) -> T {
    let controller = self.instantiateViewController(withIdentifier: identifier) as! T
    DispatchQueue.main.async {
        controller.removeButtonTitle()
    }
    return controller
}

答案 1 :(得分:0)

instantiateVC不应返回值。您需要向此函数添加新参数以传递controller

func instantiateVC<T: UIViewController>(identifier: String = T.identifier, completion: @escaping (T) -> Void) {
    // swiftlint:disable force_cast
    DispatchQueue.main.async {
        let controller = self.instantiateViewController(withIdentifier: identifier) as! T
        controller.removeBackButtonTitle()
        completion(controller)
    }
    // swiftlint:enable force_cast
}