Reputation: 1323
I am trying to initialize a ViewController by passing a View Model as shown below:
import Foundation import UIKit
class ViewController: UIViewController {
private var vm: LoginViewModel
init(vm: LoginViewModel) {
self.vm = vm
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
The init?(coder: NSCoder) initializer is giving error since the vm property is not initialized. How can I workaround this problem?
Upvotes: 0
Views: 735
Reputation: 947
You can get around the problem by adding a !
to your property. This basically makes it so that you can initialize without setting the property. The property will be nil if you don't assign a value to it.
private var vm: LoginViewModel!
Note that if you initialize via storyboard, you need to make sure you find a way to initialize the property via a segue.
Upvotes: 2