I have a viewControllers array in navigationController, and I need to find the certain vc and call popToViewController method. But type checking always failed.
How can I check if a vc is of a given type in Swift?
func popToViewController<T>(vcClass: T.Type, animated: Bool) {
guard let mainVc = self.rootController else {
return
}
let controllers = mainVc.viewControllers
for vc in controllers {
if vc is T { // always is false
_ = mainVc.popToViewController(vc , animated: true)
}
}
}
self?.router.popToViewController(vcClass: ViewController1.Type.self, animated: true)