我剛升級到Xcode 9.0 Beta,現在我的應用程序在啓動時崩潰。Xcode 9.0 Beta:當調用Type.init()時,swift「無法初始化類對象」
2017-06-09 14:35:18.817213-0700 recharge-consumer-ios[13524:1720597] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[recharge_consumer_ios.SignedOutContainerViewController<0x105c691b8> init]: cannot init a class object.'
*** First throw call stack:
(
0 CoreFoundation 0x000000010caf4f6b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x000000010ba3e121 objc_exception_throw + 48
2 CoreFoundation 0x000000010cb7f6ff +[NSObject(NSObject) init] + 127
3 recharge-consumer-ios 0x0000000105322223 _T021recharge_consumer_ios22RechargeViewControllerCACycfCTD + 19
是失敗的(簡化)呼叫:
func setAndRefreshChildViewControllersWithTypes(_ types: [MyViewControllerProtocol.Type]) {
for type in types {
let controller = type.init() as! UIViewController // This is what crashes
}
...
}
其中MyViewControllerProtocol
是:
protocol MyViewControllerProtocol {
// some other fields here too
init()
}
,這是失敗的樣子實例:
class SignedOutContainerViewController: MyViewController {
required init() {
super.init(nibName: "SignedOutContainerViewController")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
其中MyViewController
是:
class MyViewController: UIViewController, MyViewControllerProtocol {
required init() {
fatalError("init has not been implemented")
}
init(nibName nibNameOrNil: String?) {
super.init(nibName: nibNameOrNil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
任何想法發生了什麼問題,或指向哪裏可以看? Xcode的9.0 Beta版(9M136h),迅速3.
沒有攝製,向我們展示你如何申報在貼合類,以及如何你稱之爲方法。 – Kevin
@Kevin更新了完整的層次結構。該調用是第一個代碼塊中的「type.init()」。 – Eric
如果您改用MyViewController.Type,它是否仍然崩潰? –