Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't figure out why I am getting "Class ViewController has no initializers" Error in Swift iOS App Code

I was trying to make a tutorial screen in my iOS app where the user swipes past a few pictures to learn a bit about the said application. Everything looks fine, except when I build my project, I get the following error on the very first line of code:

Class ViewController has no initializers

What did I do wrong?

My ViewController.swift code is below

import UIKit

//line below is where I get the error

class ViewController: UIViewController, UIPageViewControllerDataSource {

//end

var pageViewController: UIPageViewController
var pageTitles: NSArray!
var pageImages: NSArray!

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.addChildViewController(self.pageViewController)
    self.view.addSubview(self.pageViewController.view)
    self.pageViewController.didMoveToParentViewController(self)

}

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

@IBAction func restartAction(sender: AnyObject) {
}

func viewControllerAtIndex(index: Int) -> ContentViewController
{

    if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count))
    {
        return ContentViewController()
    }

    var 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

}

// Mark: - Page View Controller Data Source

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

    var vc = viewController as! ContentViewController
    var index = vc.pageIndex as Int

    if (index == 0 || index == NSNotFound)
    {
        return nil
    }

    index--
    return self.viewControllerAtIndex(index)

}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

    var vc = viewController as! ContentViewController
    var index = vc.pageIndex as Int

    if (index == NSNotFound)
    {
        return nil
    }

    index++

    if (index == self.pageTitles.count)
    {
        return nil
    }

    return self.viewControllerAtIndex(index)
}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
    return self.pageTitles.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
    return 0
}

}
like image 491
Ahad Sheriff Avatar asked Jul 06 '15 20:07

Ahad Sheriff


2 Answers

It's because of this line:

var pageViewController: UIPageViewController

You're saying that pageViewController cannot be null, but you don't have an initializer that sets it to a value. Looking at your code, I suspect you want to change it to this:

var pageViewController: UIPageViewController!

I went into more detail on this problem here.

like image 78
TwoStraws Avatar answered Nov 14 '22 01:11

TwoStraws


In a Swift class each non-optional declared variable must be initialized in one of the given init methods.

In your case, it's this variable

 var pageViewController: UIPageViewController

declare it as implicit unwrapped optional as the other two variables, because the variable is guaranteed to be set before it's used.

 var pageViewController: UIPageViewController!
like image 28
vadian Avatar answered Nov 13 '22 23:11

vadian