Simple question really; is there a difference between these values (and is there a difference between BOOL and bool)? A co-worker mentioned that they evaluate to different things in objective-c, but when i looked at the typedefs in their respective .h files, YES/TRUE/true were all defined to 1 and NO/FALSE/false were all defined to 0. Is there really a difference?
views:
1621answers:
4
+5
A:
No there is not. C processes boolean expressions based on whether they evaluate to 0 or not 0. So:
if(someVar) { ... }
if(!someVar) { ... }
means the same as
if(someVar!=0) { ... }
if(someVar==0) { ... }
which is why you can evaluate any primitive type or expression as a boolean test (including, e.g. pointers).
Software Monkey
2009-03-05 17:22:41
+1
A:
I think they add YES/NO to be more self-explanatory in many cases. For example:
[button setHidden:YES];
sounds better than
[button setHidden:TRUE];
Marco
Marco
2009-03-05 17:29:25
+2
A:
You might want to read the answers to this question. In summary, in Objective-C (from the definition in objc.h):
typedef signed char BOOL;
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
// even if -funsigned-char is used.
#define OBJC_BOOL_DEFINED
#define YES (BOOL)1
#define NO (BOOL)0
Barry Wark
2009-03-05 17:57:23