Swift 2无法将'[AnyObject]'类型的值转换为预期的参数类型'[UIViewController]?'

Aha*_*iff 3 uiviewcontroller ios swift swift2

我正在研究Swift中的一个项目,我只是自动将代码转换Swift 1为Swift 2.但是我收到错误:

无法将'[AnyObject]'类型的值转换为预期的参数类型'[UIViewController]?'

在下面的代码中:

self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)

我原本以为如果我说的话问题会解决,as!而不是as当我这样做时,我收到了以下信息:

从'NSArray'到'[AnyObject]'的强制演员总是成功; 你的意思是用'as'吗?

接下来是这个错误:

无法将'[AnyObject]'类型的值转换为预期的参数类型'[UIViewController]?'

这是我的完整上下文代码:

    override func viewDidLoad()
{
    super.viewDidLoad()

    self.pageTitles = NSArray(objects: "", "", "")
    self.pageImages = NSArray(objects: "page1", "page2", "page3")

    self.pageViewController = self.storyboard?.instantiateViewControllerWithIdentifier("PageViewController") as! UIPageViewController
    self.pageViewController.dataSource = self

    var startVC = self.viewControllerAtIndex(0) as ContentViewController
    var viewControllers = NSArray(object: startVC)

    self.pageViewController.setViewControllers(viewControllers as! [AnyObject], direction: .Forward, animated: true, completion: nil)

    self.pageViewController.view.frame = CGRectMake(0, 30, self.view.frame.width, self.view.frame.size.height - 60)

    self.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)

    // Below is for advertisements

    self.canDisplayBannerAds = true
    self.addBannerView?.delegate = self
    self.addBannerView?.hidden = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func restartAction(sender: AnyObject)
{
    var startVC = self.viewControllerAtIndex(0) as ContentViewController
    var viewControllers = NSArray(object: startVC)

    self.pageViewController.setViewControllers(viewControllers as! [AnyObject], direction: .Forward, animated: true, completion: nil)
}

func viewControllerAtIndex(index: Int) -> ContentViewController
{
    if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
        return ContentViewController()
    }

    let vc: ContentViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController

    vc.imageFile = self.pageImages[index] as! String
    vc.titleText = self.pageTitles[index] as! String
    vc.pageIndex = index

    return vc


}
Run Code Online (Sandbox Code Playgroud)

显然我对此错误非常困惑,任何帮助将不胜感激.

谢谢Ahad Sheriff

jtb*_*des 6

在Swift 2中,此函数声明为:

func setViewControllers(_ viewControllers: [UIViewController]?,
              direction direction: UIPageViewControllerNavigationDirection,
               animated animated: Bool,
             completion completion: ((Bool) -> Void)?)
Run Code Online (Sandbox Code Playgroud)

该viewControllers参数现在是a [UIViewController].所以viewControllers你传入的数组必须是[UIViewController].您没有显示它来自何处的上下文,但您可以将基础变量更改为正确的类型,或者在使用它时将其强制转换.