Swift 2:"Bool"不可转换为"BooleanLiteralConvertible" [英] Swift 2: "Bool' is not convertible to 'BooleanLiteralConvertible'
问题描述
我在XCode 6
中创建了一个应用.今天,我下载了XCode 7
,它已将我的应用程序更新为Swift 2
.有很多错误,但是现在只有一个我无法解决.
我不知道为什么,但是Xcode
不喜欢animated
的任何Bool
选项并显示此错误-
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 -
布尔"不能转换为"BooleanLiteralConvertible"
'Bool' is not convertible to 'BooleanLiteralConvertible'
(如果您看一下函数本身,就会发现,它完全用Bool
代替animated
)
(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?
谢谢.
推荐答案
Swift令人困惑,并给您不正确的错误消息.问题在于第一个参数的类型为[UIViewController]?
,因此以下代码应该起作用:
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)
甚至更好,将viewControllers
声明为[UIViewController]
类型,然后在调用中不需要强制转换:
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)
这篇关于Swift 2:"Bool"不可转换为"BooleanLiteralConvertible"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!