gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:07:27 25 4
gpt4 key购买 nike

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

Cannot convert value of type '[AnyObject]' to expected argument type '[UIViewController]?'

在下面的代码中:

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

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

Forced cast from 'NSArray' to '[AnyObject]' always succeeds; did you mean to use 'as'?

接着是这个错误:

Cannot convert value of type '[AnyObject]' to expected argument type '[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


}

显然,我对这个错误感到很困惑,我们将不胜感激。

谢谢,阿哈德警长

最佳答案

在 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]。您没有显示它来自何处的上下文,但是您可以将基础变量更改为正确的类型,或者在使用它时对其进行强制转换。

关于ios - Swift 2 无法将类型 '[AnyObject]' 的值转换为预期的参数类型 '[UIViewController]?',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32709429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com