简体   繁体   English

Swift 2:“Bool”不能转换为'BooleanLiteralConvertible'

[英]Swift 2: "Bool' is not convertible to 'BooleanLiteralConvertible'

I created an app in XCode 6 . 我在XCode 6创建了一个应用程序。 Today I downloaded XCode 7 and it had updated my app to Swift 2 . 今天我下载了XCode 7 ,它已将我的应用程序更新为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 - 我不知道为什么,但Xcode不喜欢animated任何Bool选项并显示此错误 -

'Bool' is not convertible to 'BooleanLiteralConvertible' 'Bool'不能转换为'BooleanLiteralConvertible'

(if you look at the function itself, you will see, that it takes exactly the Bool for animated ) (如果你看一下这个函数本身,你会看到它完全需要animatedBool

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. Swift很困惑,并给你一个不正确的错误信息。 The problem is that the first parameter is of type [UIViewController]? 问题是第一个参数是[UIViewController]?类型[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: 或者甚至更好,将viewControllers声明为[UIViewController]类型,然后在调用中不需要转换:

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: Swift 1 声明 - setViewControllers:direction:animated:completion:已更改为:

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]? 所以你的施法混淆斯威夫特2,因为类型[AnyObject]viewControllers不匹配[UIViewController]? . Expect more Objective-C APIs to be audited in the future. 期待将来审核更多Objective-C API。

First fix viewControllerAtIndex to return a UIViewController : 首先修复viewControllerAtIndex以返回UIViewController

func viewControllerAtIndex(index: Int) -> UIViewController {
    ...
}

then just let Swift infer the correct types: 然后让Swift推断出正确的类型:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM