2017-06-09 47 views
0

我剛升級到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.

+0

沒有攝製,向我們展示你如何申報在貼合類,以及如何你稱之爲方法。 – Kevin

+0

@Kevin更新了完整的層次結構。該調用是第一個代碼塊中的「type.init()」。 – Eric

+0

如果您改用MyViewController.Type,它是否仍然崩潰? –

回答

0

使用@objc針對崩潰

2

我想跟進,因爲公認的答案並沒有爲我工作的方法。所有這些都在6/12/17使用Xcode的swift 4.0開發快照工具鏈在遊樂場中進行了測試。

這將失敗,運行時異常

import Foundation 

public protocol MyProtocol { 
    init() 
} 

@objc // This doesn't work 
public class MyClass: NSObject, MyProtocol { 

    @objc // This doesn't work either 
    required public override init() { 
     super.init() 
    } 
} 

let anonymousType: AnyClass = MyClass.self 

let metaType: MyProtocol.Type = anonymousType as! MyProtocol.Type 
let instance = metaType.init() 

對於我來說,這是不是標誌着與@objc類或方法簡單。我發現了兩種解決方案可以爲我工作。

  1. 馬克級決賽

    import Foundation 
    
    public protocol MyProtocol { 
        init() 
    } 
    
    final public class MyClass: NSObject, MyProtocol { 
    
        required public override init() { 
         super.init() 
        } 
    } 
    
    let anonymousType: AnyClass = MyClass.self 
    
    let metaType: MyProtocol.Type = anonymousType as! MyProtocol.Type 
    let instance = metaType.init() 
    
  2. 馬克協議@objc

    import Foundation 
    
    @objc 
    public protocol MyProtocol { 
        init() 
    } 
    
    public class MyClass: NSObject, MyProtocol { 
    
        required public override init() { 
         super.init() 
        } 
    } 
    
    let anonymousType: AnyClass = MyClass.self 
    
    let metaType: MyProtocol.Type = anonymousType as! MyProtocol.Type 
    let instance = metaType.init() 
    
相關問題