views:

1621

answers:

4

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?

+2  A: 

No, YES/NO is a different way to refer to TRUE/FALSE(1/0)

Marco

Marco
+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
+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
+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