"unwrapping an Optional value Swift" error in hiding label

Asked

Viewed 40 times

0

Every time having to hide one label of my screen using label.isHidden = true this error appears, I’ve seen in other questions here on the forum on the topic, however I could not understand correctly.

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
@IBOutlet weak var txtHE: UILabel!
@IBOutlet weak var txtHS: UILabel!
@IBOutlet weak var txtHT: UILabel!
@IBOutlet weak var txtHL: UILabel!


var esconder:Bool!

override func viewDidLoad() {
    super.viewDidLoad()

    esconder = true

    txtHE.isHidden = esco
    txtHS.isHidden = esco
    txtHT.isHidden = esco
    txtHL.isHidden = esco

}

1 answer

2


What is happening is that your Label has not yet been loaded into memory and how you put a ! in her declaration, the compiler tries to access the Label, and gives error. This exclamation indicates that the Label can never be null.

Check if the Label is being loaded correctly in the Interface Builder and check if the Label is not null before trying to access it.

As good practice, do not declare how ! and yes with ? and do a check every time you access the Label.

  • Okay, I’ll check it out.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.