1

I'm attempting to initialize this ViewController class. I am not using the MVC design strategy so ignore the bad conventions used (if any).

How do I initialize this class properly?

Error: 'required' initializer 'init(coder:)' must be provided by subclass of 'UIViewController'

Context: This is a calculator app that when any of the buttons are pressed. It will go find the senders title and simply put if one of the three vars are nil, it will store it in that optional.

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBOutlet weak var answerLabel: UILabel!
    //global vars for all funcs
    var selection1: Int? {
        didSet { answerLabel.text = String(selection1!) }
    }
    var selection2: String? {
        didSet { answerLabel.text = selection2! }
    }
    var selection3: Int? {
        didSet { answerLabel.text = String(selection3!) }
    }

    var answer: Int {
        didSet { answerLabel.text = String(answer) }
    }


    init() {


    }



    @IBAction func touchButton(_ sender: UIButton) {
        if selection1 == nil {
            selection1 = Int(sender.currentTitle!)
            print("Selection set in first pos.")
        } else if selection2 == nil {
            selection2 = sender.currentTitle
        } else if selection3 == nil {
            selection3 = Int(sender.currentTitle!)
        } else {
            calculate(firstNum: selection1!, operation: selection2!, secondNum: selection3!)
        }
    }

    func calculate(firstNum: Int, operation: String, secondNum: Int) {
        switch operation {
        case "+":
            answer = firstNum + secondNum
        case "-":
            answer = firstNum - secondNum
        case "x":
            answer = firstNum * secondNum
        case "/":
            answer = firstNum / secondNum
        default:
            answerLabel.text = "Something went wrong!"
        }
    }


}
  • because I am using optionals where the vars may not contain a value, i am required to init the class somehow –  Mar 28 '19 at 22:19
  • Duplicate -- https://stackoverflow.com/questions/30679129/how-to-write-init-methods-of-a-uiviewcontroller-in-swift – Mocha Mar 28 '19 at 22:25
  • You can avoid creating an init by setting a default value for your properties.. E.g.. var answer: Int = 0 { ... } – Mocha Mar 28 '19 at 23:24

2 Answers2

0

Your controller is being instantiated from the storyboard. A safe place to configure initial views is during the controller's call to viewDidLoad, ie:

override func viewDidLoad() {
    super.viewDidLoad()
    // configure your views and subviews here
}
eeekari
  • 202
  • 2
  • 7
0

Initialization depends on a couple of condition.

  1. If you are using storyboard, you can just remove the init and your VC will have default initializer. Make sure either all of your properties have default value or they are optional.
  2. If you are using xib or just creating view programmatically you can have custom convenience initializer where you pass some extra data this way.
class MyViewController: ViewController {
   var answer: Int
   convenience init(answer: Int) {
       self.init()

       self.answer = answer
       // Do other setup
   }
}
Aks
  • 1,567
  • 13
  • 23