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!

4 Answers 4

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)
    }
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

why is one override and the other required?
@MichaelDautermann I just added some information about required and override. Thanks for highlight.
Thanks for reply !
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")
}

Comments

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)]
}

1 Comment

That type should be [(q: String, r: String)] for qr - you're missing a bracket
-3

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.