fatal error: unexpectedly found nil while unwrapping an Optional value
some times the problem that you have initiate view controller before present it like this:
let myViewController = MyViewController()
replace that by
let myViewController = self.storyboard?.instantiateViewController(withIdentifier: "storyboardID") as! MyViewController
In my case, I was trying to access the labels in a function that I created which was used after the set of a variable.
var something: String {
didSet {
updateUI()
}
}
func updateUI() {
label.text = "Hello"
}
I solved it by:
var something: String
override func viewDidLoad() {
label.text = "hello"
}
My theory is that my function updateUI was accessed before the views and labels were created.
the error refers to the fact that you're accessing the parameter of an optional value when the optional value is set to nil (e.g. accessing answerField.text
when answerField
is nil), likely one of your two UILabels
.
If the line operaterLabel.text == ""
is throwing the exception, then your operaterLabel
is nil. Verify that you have connected it successfully to the label in your Interface Builder file.