Check the condition and store the value from user default into variable and assign to textfield.
Initial condition in user default it will be empty I am checking the condition using guard if its empty (nil) assigning cost value to "" empty String else assign the value from user default which its stored from given defaultskey.cost
class FirstViewControll : ViewController
var costValue: String?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
costValue! = userDefault.value(forKey: defaultsKeys.cost) as! String
guard costValue != nil else {
self.textField?.text = ""
return
}
self.textField?.text = costValue!
}
App crashes for failing in optional value unwrap Fatal error: Unexpectedly found nil while unwrapping an Optional value
I assume that written a valid condition code.