scribblelover27 scribblelover27 - 7 months ago 23
Swift Question

Guard statement in swift for error handling

I tried running below statement, but it's skipped right over in the code.

guard let num1 = num1Input.text else
{
show("No input in first box")
return
}


Can somebody tell me why this statement isn't running when the text field is blank?

Answer

You could use the where clause to check for non-nil and non-empty

guard let num1 = num1Input.text where !num1.isEmpty else {
    show("No input in first box")
    return
}