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

烈酒焚心 提交于 2019-12-23 09:26:48

问题


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.


回答1:


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)



回答2:


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)


来源:https://stackoverflow.com/questions/31637106/swift-2-bool-is-not-convertible-to-booleanliteralconvertible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!