Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics Introduction to Operators and Conditionals Comparison Operators

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

BOOL or bool -- Can someone give me a 'Cole's Notes' version?

I've gone through Stack Overflow and a few other tech sites. I understand the concept with the differences between BOOL (all caps) and bool (lower case), but I'm not sure as to why or when the practical application comes in. BOOL returns "YES" or "NO" and bool returns "true" or "false." How is one better?

Why / When would one be better than the other?

Thanks everyone.

cc. Gabe Nadel

1 Answer

I had the same question, Jason. This article, along with these StackOverflow answers, cleared up my confusion somewhat. I'd still appreciate some insight from Gabe Nadel, though.

The consensus seems to be that BOOL should be the type to exclusively use within Objective-C and that when you do use it, it should always be set to YES or NO. The reason for this, as I understand it, is that BOOL is a signed char (8-bits) and that this leads to ambiguity on evaluation. The fact that it's a signed char makes it so that BOOL could evaluate to any one of 256 different integers, ranging from -128 to +127. All of those integers except zero would evaluate to true; zero would obviously evaluate to false.

The issue arises whenever you try to evaluate a value greater than 256 bits — the maximum size of a signed char. When you try to do this, BOOL will truncate the extra bits and potentially evaluate to zero, to false. This would be incorrect since what you were expecting is a truthy value. Like in this pic from BigNerdRanch:

For this reason, I'm of the understanding that BOOL should always be used in Obj-C — as opposed to bool — but that it should be explicitly set to a YES or a NO value, which evaluate to one and zero, respectively. Like I said though, I still feel like I'm missing some crucial piece of the puzzle and any input from Gabe would be much appreciated.

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hey Mikis

Thanks for the links and the answer. It did clear up a bit of what I had researched.

I guess I just got further confused, because Gabe Nadel would use both sets interchangeably with no discernible pattern or reason. There were even a few points in the videos where BOOL was used with "true" and bool was used with "YES", which just threw even more confusion into the mix.

But thank you, your links did help. :smiley:

:dizzy: