Ahad Sheriff Ahad Sheriff - 6 months ago 117
iOS Question

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
}

}

Answer

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.