Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Jason AndersTreehouse Moderator 145,631 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?
cc. Gabe Nadel
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.