I created an app in XCode 6
. Today I downloaded XCode 7
and it had updated my app to Swift 2
. There were a lot of errors, but now there is only one that I can't solve.
I don't know why, but Xcode
doesn't like any Bool
option for animated
and show this error -
'Bool' is not convertible to 'BooleanLiteralConvertible'
(if you look at the function itself, you will see, that it takes exactly the Bool
for animated
)
var startVC = self.viewControllerAtIndex(indexImage) as ContentViewController
var viewControllers = NSArray(object: startVC)
self.pageViewContorller.setViewControllers(viewControllers as [AnyObject], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
'Bool' is not convertible to 'BooleanLiteralConvertible'
Does anybody know, how can I solve it?
Thanks.
Swift is confused and giving you an incorrect error message. The problem is that the first parameter is of type [UIViewController]?
, so the following should work:
self.pageViewContorller.setViewControllers(viewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
Or even better, declare viewControllers
to be of type [UIViewController]
then no casting is needed in the call:
let viewControllers:[UIViewController] = [startVC]
self.pageViewContorller.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
Try to avoid casting if possible. The Swift 1 declaration for - setViewControllers:direction:animated:completion:
has 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 cast confuses Swift 2 because the type [AnyObject]
of viewControllers
doesn't match [UIViewController]?
. Expect more Objective-C APIs to be audited in the future.
First fix viewControllerAtIndex
to return a UIViewController
:
func viewControllerAtIndex(index: Int) -> UIViewController {
...
}
then just let Swift infer the correct types:
let startVC = viewControllerAtIndex(indexImage)
let viewControllers = [startVC]
pageViewController.setViewControllers(viewControllers,
direction: .Forward, animated: true, completion: nil)
which is the readable version of:
let startVC: UIViewController = viewControllerAtIndex(indexImage)
let viewControllers: [UIViewController] =
Array<UIViewController>(arrayLiteral: startVC)
pageViewController.setViewControllers(viewControllers,
direction: UIPageViewControllerNavigationDirection.Forward,
animated: true, completion: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With