I want to pass viewController type as a function just like the code below
extension UIViewController {
func switchScreen(storyboardName:String,viewControllerName:String,vcc:UIViewController) {
let mainStoryboard = UIStoryboard(name: storyboardName, bundle: Bundle.main)
if let viewController = mainStoryboard.instantiateViewController(withIdentifier: viewControllerName) as? vcc {
self.present(viewController, animated: true, completion: nil)
}
}
}
but xcode returns an error that says Cannot find type 'vcc' in scope
I tried adding .Type
in the parameter after UIViewController
but the error is the same.
any solutions?
2
Answers
Use generic param
The third parameter must be a type, not an instance
and call it
But as
instantiateViewController
and alsopresent
don’t care about the static type you don’t need it, this is sufficientTo avoid a crash you could add an enum with the available view controller identifiers and pass this type in the second parameter for example