日々精進

新しく学んだことを書き留めていきます

super.initをイニシャライザで呼ぶと「property not initialized at super.init call」エラー

エラーメッセージの通り、super.initを呼ぶ前にすべてのプロパティを初期化してないといけない。
でもsuper.initしないとbaseクラスのメソッドが使えないので、UIViewControllerのサブクラスのイニシャライザは以下のようになる。

    init() {
        self.homeViewController = SCHHomeViewController()
        super.init(nibName:nil, bundle:nil)
        
        self.addChildViewController(self.homeViewController)
        self.homeViewController.didMoveToParentViewController(self)
    }

参考:properties - Error in Swift class: Property not initialized at super.init call - Stack Overflow