Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switch condition has boolean value?

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;
    }
}
like image 488
Edward Hasted Avatar asked Apr 14 '26 05:04

Edward Hasted


2 Answers

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.

like image 54
Rob Avatar answered Apr 16 '26 20:04

Rob


I suggest you use a simple if/else in this scenario, as is :

if (buttonstatus){
// TRUE code  
}
else{
// FALSE code
}
like image 29
Gil Sand Avatar answered Apr 16 '26 21:04

Gil Sand