Just wondering – given that I have a typedef enum ‘color’ (and property of the same name) with a valid element ‘blue’, why is this ok:
BOOL isBlue;
if (color == blue){
isBlue = YES;
}
but this is not:
BOOL isBlue;
isBlue = (color == blue);
I’ve just started using enums so maybe i’m missing something simple?
Edit – as mentioned ‘blue’ is one of the valid elements of the enum, not a BOOL itself, i.e:
typedef enum { Blue, Red, Yellow } color;
and
@property color color;
Edit 2 – here’s my actual code, as requested. I’m a bit confused by the contradictory comments/answers. Should I expect this to compile (it doesn’t)?
.h
typedef enum { AddRecipes, ManageRecipes, RemoveRecipes } mode;
<snip>
@property mode mode;
.m
@synthesize mode;
<snip>
BOOL modeIsAddRecipe = (mode == AddRecipes);
Edit 3 – for posterity I should mention that my error was trivial and unrelated. The discussion below proved enlightening though, many thanks.
As @tia already told you,
BOOLis defined assigned char, andYESandNOare defined as(BOOL)1and(BOOL)0, respectively.It’s for that reason that both code samples you show in your question will work just fine. In the latter snippet, the equality expression evaluates to either 1 or 0. You can freely assign these integer values to a
BOOL(signed char) value.The code will compile and run successfully, with neither an error nor a misbehavior.
If you’re seeing something different, please edit your question to include your actual code and the error it produces, because the code you provided works just fine.
Edit (now that you’ve provided the actual code): I think it should work, but it currently doesn’t (as of Xcode 4.3.1) because Clang is interpreting “
mode” as the typemode, not the instance variablemode. If you rename the type toRecipeModeor change the expression to either explicitly reference the instance variable (self->mode) or use the property (self.mode), it compiles fine.So, congratulations. You’ve actually (in my opinion) found a compiler bug! I suggest reporting it.