There are two problems:
- Binary operators (such as
||
) must have whitespace on both sides
(or on none), that's what causes the syntax error. (See what are the rules for spaces in swift.)
if (Int(txta.stringValue))! < 0
cannot be used to check for integer
input because the forced unwrap crashes at runtime if the string is
not an integer.
Int(txta.stringValue)
returns nil
if the string is not an integer, so
a possible check for integer input would be
if Int(txta.stringValue) == nil || Int(txtb.stringValue) == nil {
// display error message
}
But most probably you need the integer values themselves (in the case of valid input),
that's what optional binding is for
(see also When should I compare an optional value to nil?):
if let a = Int(txta.stringValue), let b = Int(txtb.stringValue) {
// Valid input, values are in `a` and `b` ...
} else {
// display error message
}
Additional conditions can be added to check the valid range, e.g.
if let a = Int(txta.stringValue), let b = Int(txtb.stringValue), a > 0, b > 0 {
// Valid positive input, values are in `a` and `b` ...
} else {
// display error message
}
Or with guard
:
guard let a = Int(txta.stringValue), let b = Int(txtb.stringValue), a > 0, b > 0 else {
// display error message
return
}
// Valid positive input, values are in `a` and `b` ...