Avoid casting whenever possible. Swift 1 declaration for - setViewControllers:direction:animated:completion: changed from:
func setViewControllers(_ viewControllers: [AnyObject]!, direction direction: UIPageViewControllerNavigationDirection, animated animated: Bool, completion completion: ((Bool) -> Void)!)
to
func setViewControllers(viewControllers: [UIViewController]?, direction: UIPageViewControllerNavigationDirection, animated: Bool, completion: ((Bool) -> Void)?)
so your throw confuses Swift 2 because the type [AnyObject] of viewControllers does not match [UIViewController]? . Expect more Objective-C APIs to be tested in the future.
First fix viewControllerAtIndex to return a UIViewController :
func viewControllerAtIndex(index: Int) -> UIViewController { ... }
then just let Swift output the correct types:
let startVC = viewControllerAtIndex(indexImage) let viewControllers = [startVC] pageViewController.setViewControllers(viewControllers, direction: .Forward, animated: true, completion: nil)
which is a readable version:
let startVC: UIViewController = viewControllerAtIndex(indexImage) let viewControllers: [UIViewController] = Array<UIViewController>(arrayLiteral: startVC) pageViewController.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
source share