No Crash textField

Is there a way for the app not to crash if the user taps on 2 or more textFields without filling them in with numbers?
Answered by Claude31 in 649621022
Could you show the code where you crash ?
And detail the crash scenario:
  • leaving fields empty ?

  • entering text when number is expected ?

Maybe you call some textField property that may be nil, like
Code Block
            let text = textField!.text

If so, use optional chaining:
Code Block
            if let text = textField?.text ?? ""

Or maybe you try to convert to Int something that is not
textField!.text.

Code Block
let value = Int(textField!.text!)

If so replace with

Code Block
let value = Int(textField!.text!) ?? 0




Accepted Answer
Could you show the code where you crash ?
And detail the crash scenario:
  • leaving fields empty ?

  • entering text when number is expected ?

Maybe you call some textField property that may be nil, like
Code Block
            let text = textField!.text

If so, use optional chaining:
Code Block
            if let text = textField?.text ?? ""

Or maybe you try to convert to Int something that is not
textField!.text.

Code Block
let value = Int(textField!.text!)

If so replace with

Code Block
let value = Int(textField!.text!) ?? 0




Thanks again it was leaving the fields empty.
No Crash textField
 
 
Q