In Objective-C I used to override the init
method of my UIViewController
. I cannot achieve the same in Swift :
Objective-C code :
- (instancetype)init
{
self = [super init];
if (self) {
self = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ViewController"];
}
return self;
}
If I try to do so in Swift I get errors for "Cannot assign to self". I have already implemented the initWithCoder:
method just to get through a few more errors.
Can someone please tell me how can I port the above Objective-C code to Swift to get the desired behavior.
I want to init the ViewController from storyboard.
You cannot assign
self
ininit
. You should useclass
method or public function instead.After you can call