So I have a class with a designated initializer that takes values for every stored property. All my stored properties also have a default values, so I assume this class get a default init.
In my designated init, I call super.init()
The problem is, if I call it at the end of my init, it loads all properties to default values, but if I call it at the start it works as I expect.
The book says:
Safety check 1 A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.
As mentioned above, the memory for an object is only considered fully initialized once the initial state of all of its stored properties is known. In order for this rule to be satisfied, a designated initializer must make sure that all its own properties are initialized before it hands off up the chain.
So I am not sure if things have changed or I am doing something wrong?
Code:
class ORQuizViewController: UIViewController {
let imageView: UIImageView = UIImageView(image: nil)
let questionLabel: UILabel = UILabel()
let choicesArray: [BlackWhiteButton] = [BlackWhiteButton]()
let correctAnswer: Int = -1
init(image: UIImage!, question: String, choices: [String], answerIndex: Int) {
super.init()
imageView = UIImageView(image:image)
questionLabel = UILabel()
questionLabel.text = question
var tempChoices = [BlackWhiteButton]()
for choice in choices {
var choiceLabel = BlackWhiteButton()
choiceLabel.setTitle(choice, forState: .Normal)
tempChoices.append(choiceLabel)
}
choicesArray = tempChoices
correctAnswer = answerIndex
}