【问题标题】:Swift 2: "Bool' is not convertible to 'BooleanLiteralConvertible'Swift 2:“Bool”不能转换为“BooleanLiteralConvertible”
【发布时间】:2015-07-26 12:46:36
【问题描述】:

我在XCode 6 中创建了一个应用程序。今天我下载了XCode 7,它已将我的应用程序更新为Swift 2。有很多错误,但现在只有一个我无法解决。 我不知道为什么,但 Xcode 不喜欢 animated 的任何 Bool 选项并显示此错误 -

'Bool' 不能转换为 'BooleanLiteralConvertible'

(如果您查看函数本身,您会发现,它完全采用 Bool 代替 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'

有谁知道,我该如何解决?

谢谢。

【问题讨论】:

  • 是“pageViewContorller”的拼写错误吗?

标签: ios xcode swift uipageviewcontroller swift2


【解决方案1】:

Swift 很困惑,给你一个不正确的错误信息。问题是第一个参数的类型是[UIViewController]?,所以下面应该可以工作:

self.pageViewContorller.setViewControllers(viewControllers as? [UIViewController], direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

或者更好的是,将 viewControllers 声明为 [UIViewController] 类型,然后在调用中不需要强制转换:

let viewControllers:[UIViewController] = [startVC]
self.pageViewContorller.setViewControllers(viewControllers, direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)

【讨论】:

    【解决方案2】:

    尽可能避免强制转换。 - setViewControllers:direction:animated:completion: 的 Swift 1 declaration 已从:

    func setViewControllers(_ viewControllers: [AnyObject]!,
              direction direction: UIPageViewControllerNavigationDirection,
               animated animated: Bool,
             completion completion: ((Bool) -> Void)!)
    

    到

    func setViewControllers(viewControllers: [UIViewController]?,
              direction: UIPageViewControllerNavigationDirection,
               animated: Bool,
             completion: ((Bool) -> Void)?)
    

    所以你的演员混淆了 Swift 2,因为 viewControllers 的类型 [AnyObject] 与 [UIViewController]? 不匹配。预计未来会有更多的 Objective-C API 被审计。

    首先修复viewControllerAtIndex 以返回UIViewController:

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

    然后让 Swift 推断正确的类型:

    let startVC = viewControllerAtIndex(indexImage)
    
    let viewControllers = [startVC]
    
    pageViewController.setViewControllers(viewControllers,
        direction: .Forward, animated: true, completion: nil)
    

    这是以下的可读版本:

    let startVC: UIViewController = viewControllerAtIndex(indexImage)
    
    let viewControllers: [UIViewController] =
        Array<UIViewController>(arrayLiteral: startVC)
    
    pageViewController.setViewControllers(viewControllers,
        direction: UIPageViewControllerNavigationDirection.Forward,
        animated: true, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 1970-01-01
      • 2011-09-28
      • 2015-04-25
      • 2017-06-11
      相关资源
      最近更新 更多