16

I have a problem when I build my app in swift 2. Xcode says:

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

This is the code of the class :

class creerQuestionnaire: UIView {
  @IBOutlet weak var nomQuestionnaire: UITextField!
  @IBOutlet weak var question: UITextField!
  @IBOutlet weak var reponse: UITextField!
  var QR: Questionnaire

  @IBAction func creerQuestion(sender: AnyObject) {
    QR.ajouterQuestion(question.text!, nouvReponse: reponse.text!)
  }
}

and this is the class Questionnaire:

import Foundation

class Questionnaire {
  var QR = [String(), String()]

  func getQuestion(nbQuestion: Int) ->String {
    return QR[nbQuestion]
  }

  func getReponse(nbReponse: Int) ->String {
    return QR[nbReponse]
  }

  func ajouterQuestion(nouvQuestion: String, nouvReponse: String) {
    QR += [nouvQuestion, nouvReponse]
  }
}

Merci!

pre
  • 3,475
  • 2
  • 28
  • 43
R1'
  • 490
  • 1
  • 5
  • 17

4 Answers4

26

Note for required: Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer.

Note for override: You always write the override modifier when overriding a superclass designated initializer, even if your subclass’s implementation of the initializer is a convenience initializer.

Above both notes are referred from: Swift Programming Language/Initialization

Therefore, your subclass of UIView should look similar to the sample below:

class MyView: UIView {
    ...
    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    ...
}
Allen
  • 1,714
  • 17
  • 19
3

According to the latest swift syntax, the init method needs to add methods:

required init?(coder aDecoder: NSCoder) 
{
         fatalError("init(coder:) has not been implemented")
}
Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
1

Some comments to this code:

var QR = [String(), String()]

a var name should begin with lowercase : var qr

Do you want to initialize with 2 empty strings ? In your code, you get ["", ""] at init. This array will not be very convenient to use, because you mix question and answer in sequence.

Would probably be better to create an array of pairs :

var qr : [(q: String, r: String) = [] // initiated as empty

you access to its components by : qr[i].q and qr[i].r

You need to test that nbQuestion is in bounds ; with the previous definition of qr, that would be

func getQuestion(nbQuestion: Int) ->String {
    if nbQuestion < 0 || nbQuestion >= qr.count { return "" }
    return qr[nbQuestion].q
}

Note: nbReponse and nbQuestion share the same value for a given qr

func getReponse(nbReponse: Int) ->String {
   if nbReponse < 0 || nbReponse >= qr.count { return "" }
      return qr[nbReponse].r
}

func ajouterQuestion(nouvQuestion: String, nouvReponse: String) {
    qr += [(nouvQuestion, nouvReponse)]
}
claude31
  • 874
  • 6
  • 8
-1

Non-Optional variables must be initialised with a value...

Either Declare QR as an optional

var QR: Questionnaire?

OR initialise it:

var QR: Questionnaire = Questionnaire()
Umer Hassam
  • 1,332
  • 5
  • 23
  • 42