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

时间:2015-09-22 06:00:07

标签: ios swift uiviewcontroller swift2

我正在开发一个Swift项目,我只是自动将代码从Swift 1转换为Swift 2。但是我收到了错误:

  

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

在下面的代码中:

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

我最初认为如果我说as!而不是as,问题就会解决,但当我这样做时,我收到了以下消息:

  

NSArray'强制演员阵容到[AnyObject]'永远成功;你是否   意味着使用'作为'?

后面跟着这个错误:

  

无法转换类型' [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


}

显然我对这个错误非常困惑,任何帮助都将不胜感激。

谢谢你, 阿哈德警长

1 个答案:

答案 0 :(得分:6)

在Swift 2中this function is declared as

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

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