How to set a non optional variable with a convenience initializer in swift?

493 views Asked by At

I have a UITableViewController subclass, that should not work if there is no Model. If there is no Model, there isn't really any purpose in showing the View.

So I'm thinking, my model property should not be an optional value. I want this kind of safety. So i'm trying to create a convenience init that passes my model along.

let model:Client

override init() {
    super.init(style: UITableViewStyle.Plain)
}

convenience init(model:Client) {
    self.init()
    self.model = model
}

My problem is that I'm getting this error:

Property 'self.model' not initialised at super.init call

This makes sense. If init() were to be called, the property would not be set, as required by a non-optional property.

I do I overcome this?

Bear in mind, that model is my actual Model, setting a default value here would be pointless, and again, defeat the safety that I'm looking for.

Thank you!

Small note: doing this, will not work also. There is no instance to set the model anyway.

convenience init(model:Client) {
    self.model = model
    self.init()
}

Edit: The approach bellow seemed promising

let model: Client

required init(coder aDecoder: NSCoder) {
    preconditionFailure("Cannot initialize from coder")
}

init(model:Client) {
    self.model = model
    super.init(style: UITableViewStyle.Plain)
}

however, it gets me this error:

fatal error: use of unimplemented initializer 'init(nibName:bundle:)'

And finally, the solution here was to call:

super.init(nibName: nil, bundle: nil)
1

There are 1 answers

1
Rob Napier On BEST ANSWER

You don't need to override init() here, and you want to make init(model:) your designated initializer (not a convenience). You can do that like this

let model: Client

required init(coder aDecoder: NSCoder) {
    preconditionFailure("Cannot initialize from coder")
}

init(model:Client) {
    self.model = model
    super.init(style: UITableViewStyle.Plain)
}