We usually do some setup during initialising a view controller, under UIViewController. It’s pretty simple to override init() method in objective c, like:
- (id) init {
self = [super init];
// initializer implementation goes here
return self;
}
However, in swift the story is very different. Firstly, you need to have a Required Initializer as:
class SomeClass: UIViewController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
But...but you will get into an error if you would like to initialize an instance for SomeClass without a parameter for "coder", such as:
SomeClass()
// => Missing argument for parameter 'coder' in call
So...so the completion solution is:
class SomeClass: UIViewController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init() {
super.init(nibName: nil, bundle: nil)
// initializer implementation goes here
}
You may choose to not override init() but use loadView() or viewDidLoad() instead. But they are still different from an initializer in many ways, which would be covered in later topics.