Boolean In Objective C
This post is inspired from the eye-opener article -> bignerdranch.
I always had this question, “Why does Objective-C need a
BOOL when C gives it a free
bool, which effectively works the same?”.
Well, I couldn’t have been more wrong.
bool are entirely different beasts.
BOOL is a typedef, defined at
/usr/include/objc/objc.h (Also available here)
Boolean, which is defined at
/usr/include/MacTypes.h (Also available here)
NO are macros for 1 and 0. Reference
#define YES (BOOL)1
#define NO (BOOL)0
bool is a defined type in C like
int etc. It can have either
false as value. It can have no other value. So, whenever you are writing a conditional statement, always try to use
bool instead of any of the aforementioned options.
This is required because comparisons like the one below can fail, even when you don’t expect it to. Example:
For more such examples, look at bignerdranch.
The thing to note is that, you should be careful about the same comparisons for