Have an error with the swift below code, I assume I can't use comparison operator within a 'if' statement...?
I receive the error
'Unary operator cannot be seperated from its operand' if i try to use an && or || on after the first 'else' statement.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if (row < 8)
self.pickerLabel.text = "Too cold";
else if(row >= 8 && <= 10) // Error occurs here...
self.pickerLabel.text = "Just right";
else if(row == 12)
self.pickerLabel.text = "Too warm";
view.backgroundColor = UIColor.blueColor();
Is there a work around, or solution that will get me around the error that anyone knows of?
Best How To :
Welcome to Stack Overflow.
The line should read:
else if row >= 8 && row <= 10 // No more error
(Note that you don't need parentheses around the if expression either)
With order of operations the inequalities are evaluated before the logical "AND" (&&). As Martin pointed out in his comments, logical "AND" and the other logical operators are "short circuit" operators in Swift like they are in C.
If the expression on the left of a logical AND is false, the next part of the expression isn't evaluated at all since you can be sure that the entire expression will be false.
This would be a good situation to use the swift case statement, since it allows ranges in cases.
You are missing handling for row = 11.