This code seems perfectly logical. Why am I getting the "Switch condition has boolean value" error? I'm using it to switch the literals on a UIButton. It works but I still have the error.
bool buttonSunStatus = TRUE;
- (void) useButtonSun:(id)sender
{
buttonSun = [UIButton buttonWithType: UIButtonTypeCustom];
switch (buttonSunStatus) {
case TRUE:
[sender setTitle:@"No Sun" forState:UIControlStateNormal];
buttonSunStatus = FALSE;
break;
case FALSE:
[sender setTitle:@"Sun" forState:UIControlStateNormal];
buttonSunStatus = TRUE;
break;
}
}
The warning is simply telling you that you're using switch statement with boolean value. For true boolean values, you should just use if-else statement, not switch.
Note, you will only get this warning if you use true boolean type. If you use BOOL, that's an unsigned char on Mac OS or on non-64-bit iOS. Thus you wouldn't get this warning for those targets. But if you use bool (or if you use BOOL on 64-bit iOS targets), you will receive this warning.
You can replace the switch statement with if-else statement to suppress this warning.
I suggest you use a simple if/else in this scenario, as is :
if (buttonstatus){
// TRUE code
}
else{
// FALSE code
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With