![](/img/trans.png)
[英]'required' initializer 'init(coder:)' must be provided by subclass of UIViewController
[英]UIViewController and required init?(coder: )
Xcode不斷給我一些運行時錯誤,例如“致命錯誤:init(coder :)尚未實現:file /.../ myproject.swift”
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var boardView: UIView!
@IBOutlet weak var movesLabel: UILabel!
@IBOutlet weak var timeLabel: UILabel!
let BOARD_SIZE: Int = 10
var board: Board
var squareButtons: [SquareButton] = []
required init?(coder aDecoder: NSCoder) {
self.board = Board(size: BOARD_SIZE)
super.init(coder: aDecoder)
fatalError("init(coder:) has not been implemented")
}
}
由於必須在子類中實現所需的init?(coder :),因此,我嘗試同時在所需的init中初始化board。 顯然,它不起作用。 任何建議將不勝感激。
去掉
fatalError("init(coder:) has not been implemented")
從required init?(coder aDecoder: NSCoder)
初始化程序開始的行。 此行引發您的錯誤。 不需要此行。
當您創建init?(coder aDecoder: NSCoder)
方法時,XCode會添加此行,以提醒開發人員實現此方法或永遠不要調用它。 如果開發人員忘記實現它,但嘗試從存檔或Storyboard實例化一個類,則他將收到一個斷言,並且能夠輕松地發現錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.