Try:
let currentViewController = pageViewController.viewControllers![0]
It would be safer to write:
if let currentViewController = pageViewController.viewControllers?[0] { // ... and then do everything else in the if-block end
Another alternative:
guard let currentViewController = pageViewController.viewControllers?[0] else { return }
This has the advantage that it is safe, but there is no need to put the rest of the function inside the if block.
source share