Reasons not to use _Bool in Objective-C?
I think you all but answered your own question- the reason not to use _Bool in new Cocoa code is that, until Apple changes its frameworks over to using _Bool (or more probably, the bool defined in stdbool.h), you're breaking convention and possibly compatibility (at least without hacks) by using _Bool or bool. Although I've only been steeping in Cocoa programming for a couple years now, I'd bet that if Apple incorporates _Bool at all, they will probably simply redefine the BOOL macro to use the new type behind the scenes anyway, to avoid untold editing to their framework and its documentation.
That being said, (and let me preface this by disclaiming that I've yet to mix C code in with Objective-C and don't know the conventions for doing so), you have a much better case for using the new _Bool within C functions, probably with the caveat that it is only used internally and doesn't ask an Objective-C method to pass in a _Bool, just to avoid confusion for future programmers. You'd also of course have to be comfortable with always requiring C99 compilation, which people may still have reason to avoid. Considering YES is a macro for 1 and NO is a macro for 0, there doesn't seem to be much gain in requiring a newer version of C to get another char-sized value that only uses 1 or 0.
Honestly, when it comes down to it, you can get around any of these reasons with enough hackery or restrictions placed on reusability, but the end justification is that it's not (currently) part of the Cocoa/Objective-C slang, and its benefits probably won't outweigh the loss in readability and/or added confusion of other less-privy-to-_Bool programmers reading your code.
With Objective-C, just use the BOOL data-type...
For C, I recommend the following macros, that will also work with ANSI-C (C-89):
#ifndef __bool_true_false_are_defined #ifdef _Bool #define bool _Bool #else #define bool char #endif #define true 1 #define false 0 #define __bool_true_false_are_defined 1#endif
I agree with @pst. Objective-C doesn't hide C, but it's still a layer on top of C. This means Objective-C is simply different layer.
I think this is a problem about language context. It's not always clear, but we know kind of C-context and Objective-C context. Like American
vs Americano
. They're essentially same meaning but differentiate the context, and could be different a little by the contexts. And it can make some obvious information for reader when those little details are accumulated. It will help to increase code readability.
And I believe that you should know importance of readability. If readability is not important, we don't need to use any tabs or white-spaces. :)
As another examples, there are nil
vs NULL
, strlen()
vs -[NSString length]
. BOOL
can say "I'm Objective-C code." strongly which _Bool
can't.