[英]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
) (如果你看一下这个函数本身,你会看到它完全需要
animated
的Bool
)
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.