Swift fatal error: unexpectedly found nil while unwrapping an Optional value?

Reason for error: The control may not be associated with a definition in the code.
Solution one: Second, click the control, drag the dot to the control in the interface from Show the Connections Inspector, select the correct control and make the connection. Leave the object unreleased and remove the weak keyword.
Solution 2: Use the Guard keyword for judgment.

1, the judgment is not empty
If the sender. TitleLabel. Text! = nil {
Print (” Contains a value!” )
} else {
Print (” Doesn ‘t contain a value. “)
}
             
             
2. Optional binding
If let text = sender.titlelabel.text {
Print (” Contains a value! It is \(number)!” )
} else {
Print (” Doesn ‘t contain a number “)
}
            
             
3. Guard statement
Guard let text = sender.titlelabel.text else {
The return
}

Read More: