I am new to Swift app development, so forgive me if I'm missing something simple. I created an initializer in one a QuizControl class for dependency injection. In this initializer, there is one parameter for a Questions object (an array of Question objects) and the questions are passed from another class, otherwise they're initialized to a default value.
I am also trying to set the title of the buttons and a label to the respective data from these passed Questions. The label should display the Question and the buttons should display the answers.
I believe my trouble comes from unwrapping because every time I try to initialize the text for the label or buttons, I get this error:
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
Before coming here, I tried a few solutions. I tried setting the label's text and button's text to a placeholder string that I created at initialization like so:
QLabel.text = "Question Placeholder"
for i in 0..<Buttons.count {
Buttons[i].setTitle("Answer \(i)", for: UIControl.State.normal)
}
However, I still get the same unexpectedly found nil. Then I tried declaring the QLabel and Buttons to be optional with '?' like this:
@IBOutlet var Buttons: [UIButton]?
@IBOutlet weak var QLabel: UILabel?
Instead of the way I had been trying it before:
@IBOutlet var Buttons: [UIButton]!
@IBOutlet var QLabel: UILabel!
But then I just get exactly what shows on the storyboard rather than even the placeholder strings that I created earlier. I even made sure that the same initialization values were in the viewDidLoad() for the class. Where am I going wrong? It feels as if I've tried everything.
Here are the initializers:
init(){
Questions = []
correct = 0
incorrect = 0
super.init(nibName: nil, bundle: nil)
}
init(Questions: [Question])
{
super.init(nibName: nil, bundle: nil)
self.Questions = Questions
self.correct = 0
self.incorrect = 0
QLabel.text = "Question Placeholder"
for i in 0..<Buttons.count {
Buttons[i].setTitle("Answer \(i)", for: UIControl.State.normal)
}
self.pickQuestion()
}
required init?(coder: NSCoder){
super.init(coder: coder)
}
and the viewDidLoad() of the class that is calling the initializer:
override func viewDidLoad() {
let torsoQuestions =
[Question(Question: "My chest hurts and I'm having difficulty breathing", Answers: ["Check for Asthma","Check family history of heart problems","Call an ambulance","Sample diagnosis 3"], Answer: 2),
Question(Question: "Question 2", Answers: ["Answer 4","Answer 5","Answer 6","Answer 7"], Answer: 0),
Question(Question: "Question 3", Answers: ["Answer 8","Answer 9","Answer 10","Answer 11"], Answer: 1),
Question(Question: "Question 4", Answers: ["Answer 12","Answer 13","Answer 14","Answer 15"], Answer: 2),
Question(Question: "Question 5", Answers: ["Answer 16","Answer 17","Answer 18","Answer 19"], Answer: 3)]
let quizController1 = TestQuizViewController(Questions: torsoQuestions)
}
Let me know if I am missing any important information, thank you.
RESOLVED: I realize that I was getting this error because of how objects on the storyboard like buttons and labels are loaded. I should have been passing the data through a prepare(for segue:) method to a class variable that could be used in the viewDidLoad() method for TestQuizViewController. I was getting this error because I was trying to set values for the buttons and labels that had not been loaded yet. The outlets are loaded in viewDidLoad() so I could not make changes before they had been loaded.