I am trying to figure out how to fix this issue in Swift on Xcode 7 (iOS9) and I am also having this error:
Cannot subscript a value of type '[UIViewController]?' with an index of type 'Int'
Any suggestion appreciated. Thanks.
My code:
func indexPositionForCurrentPage(pageViewController: UIPageViewController) -> Int {
let currentViewController = pageViewController.viewControllers[0] as UIViewController
for (index, page) in pages.enumerate() {
if (currentViewController == page) {
return index
}
}
return -1
}
Try:
It would be safer, though, to write:
Another alternative:
This has the advantage that it's safe but there's no need to put the remainder of the function inside an
if
block.