The Objective-C BOOL is NOT an object, it is a raw data typ an the predefined values for it are these constants: YES and NO.

You can assign a value to a BOOL variable as follows:

BOOL canVote = YES;

In general, it’s best to compare BOOL values against the value NO. Because only one value can ever indicate false. For example, you can use a Boolean value in a comparison statement, such as this one:

BOOL canVote = YES;
if (canVote == NO)
   self.outputBox.text=@"You are not old enough to vote.";
else
  self.outputBox.text=@"You are old enough to vote.";

Output
You are old enough to vote.

If you were to view the value of canVote, using this code:

self.outputBox.text=[NSString stringWithFormat:@"%i",canVote];

you will see this output:
1
That’s because Objective-C defines the value YES as 1 and the value NO as 0. When Objective-C evaluates a Boolean value in a comparison expression, it see any non-zero values as true/yes, and a value of zero as false/no. Translation, in above code you could initialize the canVote variable with any value other than the number 0 to represent true/yes, and the number 0 to represent false/no.

No Responses

Leave a Reply