3

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

1

1 Answer 1

6

In Swift 2 this function is declared as:

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

The viewControllers parameter is now a [UIViewController]. So your viewControllers array that you pass in must be [UIViewController]. You haven't shown the context of where it comes from, but you could either change the underlying variable to be the right type, or cast it when you use it.

Sign up to request clarification or add additional context in comments.

4 Comments

I have edited my question with the rest of my code for context. I still can't wrap my head around what I need to do here...Why did Swift 2 change things up?
@AhadSheriff This is nothing to do with Swift 2 but Apple has improved ti Obj-C frameworks to be properly typed for Swift. So [UIViewController] instead of just [AnyObject].
@AhadSheriff don't use NSArray(object: x). Just use [x].
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) { return nil }, how are you returning ContentViewController()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.