0

I have created a swift file MainViewController that declares/defines a class MainViewController. I've set my storyboard's only scene's custom class as this class. I have the following code:

import Foundation

class MainViewController : UIViewController {



   required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
       fatalError("init(coder:) has not been implemented")
   }

   func setup() {

      self.view.backgroundColor = UIColor.blueColor()
   }

}

When I run the project I get the following crash log:

fatal error: init(coder:) has not been implemented: file /Users/alexanderbollbach/Desktop/DraWave/content/MainViewController.swift, line 17

It seems that this is an issue people have experienced before and that swift has introduced some complexities regarding view controller initialization. I am currently learning swift but cannot resolve what the issue is. It may be worth noting that this is a mixed-language project that contains objective-c classes.

Alex Bollbach
  • 3,730
  • 2
  • 25
  • 61
  • Why not comment out fatalError? ``//fatalError("init(coder:) has not been implemented")`` – Allen Mar 13 '16 at 04:18
  • why not indeed. because i'm tired i suppose. to be fair though, the compiler did place that code there automatically. what is the purpose of the fatalerror? – Alex Bollbach Mar 13 '16 at 04:44
  • ``fatalError()`` is a way to print a message and stop execution unconditionally. Usually used to highlight a certain part for developer to address this issue. – Allen Mar 13 '16 at 04:47
  • i kinda meant what was the logic for it to be in the body of this particular method. – Alex Bollbach Mar 13 '16 at 04:50
  • You could check a reference here: http://stackoverflow.com/questions/24036393/fatal-error-use-of-unimplemented-initializer-initcoder-for-class/24036440#24036440 – Allen Mar 13 '16 at 04:58
  • Delete the entire function. – ryantxr Mar 13 '16 at 06:13

0 Answers0