我有一个 UITableViewController 子类,如果没有模型,它就不应该工作。如果没有模型,则显示视图实际上没有任何目的。
所以我在想,我的模型属性不应该是一个可选值。我想要这样的安全感。所以我正在尝试创建一个方便的 init 来传递我的模型。
let model:Client
override init() {
super.init(style: UITableViewStyle.Plain)
}
convenience init(model:Client) {
self.init()
self.model = model
}
我的问题是我收到了这个错误:
Property 'self.model' not initialised at super.init call
这是有道理的。如果要调用 init(),则不会按照非可选属性的要求设置属性。
我要克服这个吗?
请记住,该模型是我的实际模型,在此处设置默认值将毫无意义,并且再次破坏了我正在寻找的安全性。
谢谢!
小记:这样做,也不行。无论如何都没有设置模型的实例。
convenience init(model:Client) {
self.model = model
self.init()
}
编辑:下面的方法似乎很有希望
let model: Client
required init(coder aDecoder: NSCoder) {
preconditionFailure("Cannot initialize from coder")
}
init(model:Client) {
self.model = model
super.init(style: UITableViewStyle.Plain)
}
但是,它给了我这个错误:
fatal error: use of unimplemented initializer 'init(nibName:bundle:)'
最后,这里的解决方案是调用:
super.init(nibName: nil, bundle: nil)