Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2: "Bool' is not convertible to 'BooleanLiteralConvertible'

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.

like image 800
Ivan Petrov Avatar asked Jul 26 '15 12:07

Ivan Petrov


2 Answers

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)
like image 186
vacawama Avatar answered Nov 15 '22 07:11

vacawama


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)
like image 42
eik Avatar answered Nov 15 '22 06:11

eik