![](/img/trans.png)
[英]Cannot convert return expression of type 'child' to return type T
[英]Cannot convert return expression of type '()' to return type 'T'
需要為在調度主線程中實例化的視圖控制器包裝擴展,但是遇到了那個錯誤,有什么解決辦法嗎?
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()!
}
}
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
}
試試是否只去掉主線程上的按鈕標題就足夠了
func instantiateVC<T: UIViewController>(identifier: String = T.Identifier) -> T {
let controller = self.instantiateViewController(withIdentifier: identifier) as! T
DispatchQueue.main.async {
controller.removeButtonTitle()
}
return controller
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.