简体   繁体   English

如何正确覆盖UITableViewController子类的init()?

[英]How to properly override init() for UITableViewController subclass?

My UITableViewController subclass MatchTableViewController has the following properties: 我的UITableViewController子类MatchTableViewController具有以下属性:

class MatchTableViewController: UITableViewController {
    // MARK: - Properties
    var matches = [Match]()
    var dataModel: DataModel
    var apiModel: APIModel

I want to initialize dataModel and apiModel by overriding initializers. 我想通过重写初始化程序来初始化dataModelapiModel

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
    // Init the DataModel
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    guard let historyEntity = NSEntityDescription.entity(forEntityName: "History",
                                                         in: managedContext)
        else {
            fatalError("Failed to load the History entry")
    }
    self.dataModel = DataModel(historyEntity: historyEntity,
                               managedContext: managedContext)

    // Init the APImodel
    self.apiModel = APIModel()
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    // Error here
}


override init(style: UITableView.Style) {
    // Init the DataModel
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
    let managedContext = appDelegate.persistentContainer.viewContext
    guard let historyEntity = NSEntityDescription.entity(forEntityName: "History",
                                                         in: managedContext)
        else {
            fatalError("Failed to load the History entry")
    }
    self.dataModel = DataModel(historyEntity: historyEntity,
                               managedContext: managedContext)

    // Init the APImodel
    self.apiModel = APIModel()
    super.init(style: style)
    // Error here
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Xcode gives me error 'super.init' isn't called on all paths before returning from initializer right after two super.init calls. Xcode给出了错误'super.init' isn't called on all paths before returning from initializer在两个super.init调用之后立即'super.init' isn't called on all paths before returning from initializer super.init调用。 I am not sure what I am missing here. 我不确定我在这里缺少什么。

Problem is that if inside guard value isn't assigned, super.init(...) isn't called and non-optional variables aren't assigned as well. 问题是如果未分配内部guard值,则不会调用super.init(...) ,也不会分配非可选变量。 But you want to throw fatalError in else {...} so calling super.init(...) here wouldn't make any sense. 但是你想在else {...}抛出fatalError ,所以在这里调用super.init(...)是没有任何意义的。

So first call super.init(...) and then do other stuff 所以首先调用super.init(...)然后再做其他事情

override init(...) {
    super.init(...)
    ... // do other stuff
}

then make sure that you assign all non-optional global variables before you call super.init(...) . 然后确保在调用super.init(...)之前分配所有非可选的全局变量。 If don't (like your case), make these variables optional 如果不(像你的情况),使这些变量可选

var variable: Type?
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

}

when override the init method required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell' 当覆盖所需的init方法'初始化'init(编码器:)'必须由'UITableViewCell'的子类提供

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM