I am working on a project in Swift and I just automatically converted my code from Swift 1 to Swift 2. However I am getting the error:
Cannot convert value of type '[AnyObject]' to expected argument type '[UIViewController]?'
In the following piece of code:
self.pageViewController.setViewControllers(viewControllers as [AnyObject], direction: .Forward, animated: true, completion: nil)
I originally thought that the problem would be solved if I said as! rather than as but when I did this I received the following message:
Forced cast from 'NSArray' to '[AnyObject]' always succeeds; did you mean to use 'as'?
Followed by this error:
Cannot convert value of type '[AnyObject]' to expected argument type '[UIViewController]?'
Here is my entire code for context:
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
}
Obviously I am very confused by this error, any help would be appreciated.
Thank you, Ahad Sheriff