Reasons not to use _Bool in Objective-C?
Since C99, C now has a proper Boolean type, _Bool
. Objective-C, as a strict superset of C, inherits this, but when it was created back in the 1980s, there was no C Boolean type, so Objective-C defined BOOL
as signed char
.
All of Cocoa uses BOOL
, as does all non-NeXT/Apple Cocoa code that I've seen. Obviously, for compatibility with existing protocols (e.g., -applicationShouldTerminateAfterLastWindowClosed:
from NSApplicationDelegate
), matching the already-declared type is preferable, if for no other reason than to avert a warning.
For cleanliness/readability purposes, stdbool.h
defines bool
as a synonym for _Bool
, so those of us who don't want unnecessary underscores in our code can use that.
Three other useful notes:
@encode(_Bool)
evaluates to"B"
. (@encode(BOOL)
evaluates to"c"
, forsigned char
.)sizeof(_Bool)
evaluates to1
, which follows from C99's definition that_Bool
is only as large as necessary to hold its two possible values. (Edit: Actually, the standard says only that it must be “larg开发者_运维技巧e enough” to hold those two values; it does not place an upper bound, and, in fact, Mac OS X on 32-bit PowerPC defines it as 4 bytes. Size difference is another thing to file under possibleBOOL
-vs.-bool
compatibility issues.)- On that note, the only two possible values of a
_Bool
are 1 and 0. Any other values are converted to one of these on assignment, as if you had done a double-negation (!!
) or tested inequality against 0 (!= 0
). The only ways to get a_Bool
with some other value are the usual magicks: Pointer aliasing and unions.
Is there any reason not to use _Bool
/bool
in new code?
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.
精彩评论