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?
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?
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
}
You should check the length of the text, not if its nil
i.e:
guard num1Input.text.characters.count > 0 else {
...
}
If the text is optional, you can do.
guard let num1 = num1Input.text where num1.characters.count > 0 else {
...
}
This statement is testing whether the text
is nil
but you probably want to test whether the string is empty, therefore
guard let input = num1Input.text where input.characters.count > 0 else {
print("empty")
return
}
or just simply
guard num1Input.text?.characters.count > 0 else {
print("empty")
return
}