26

Is there any technical reason why Objective-C uses YES and NO instead of 1 and 0, or is it simply to make it more readable?

6 Answers 6

57

Making it more readable is a technical reason.

0
29
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

(For reference)

[button setAttr:YES];

Sounds nicer IMHO then...

[button setAttr:TRUE];
28

C (on which Objective-C is based) didn't have a boolean type until C99.

Objective-C was created in the 80s and defined it's own boolean type.

1
  • 14
    Objective-C is so 80's chic.
    – Alex Gray
    Commented Oct 1, 2013 at 12:15
22

Because the programmer means yes and no, not 1 and 0.

9

The same reason most languages use true and false... You can use 1 and 0 if you like, same as any of those other languages.

Really, if you think about it, we're talking about:

#define YES 1
#define NO  0

It's simply nicer to read.

3
  • 1
    you can still use true/false in objective-c
    – zpesk
    Commented Mar 27, 2009 at 20:09
  • 1
    Am I the ONLY one that constantly gets tripped up by this... I grew up in the bash shell.. where 0... "means good", aka YES!... á la exit 0... Every time I use a C boolean I have to force my mind to accept this paradoxical reality!
    – Alex Gray
    Commented Oct 1, 2013 at 12:19
  • @alexgray I am a "Bash"-ist too. A way to remember is: "It is true that bash shell executed correctly when there is 0 error (ie 0 == true in Bash) and while everything else think there is 1 error (ie 1 == true in non Bash language)" Commented Sep 3, 2015 at 7:40
3

It's just syntax, there's no technical reason for it. They just use YES/NO for their BOOL instead of true/false like c++ does.

1
  • 1
    Down-voted because this does not answer the question.
    – Chris Page
    Commented Jun 24, 2014 at 21:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.