I have a piece of code that runs if a switch has been set in settings as follows:
UserDefaults.standard.bool(forKey: "signatureSwitchState")
let buttonState = UserDefaults.standard.object(forKey: "signatureSwitchState") as! Bool
if buttonState == true {
sign()
}
My problem is if the switch has never been activated the program fails as the compiler states that, "fatal error: unexpectedly found nil while unwrapping an Optional value"
My question is then how best to guard against a nil value when using a bool such as the switch in the above statement.
I've tried if let statements and guard statements but the compiler complains that these cannot be used with a bool.