Note that there are some explanatory texts on larger screens.

plurals
  1. POReasons not to use _Bool in Objective-C?
    primarykey
    data
    text
    <p>Since C99, C now has a proper Boolean type, <code>_Bool</code>. 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 <code>BOOL</code> as <code>signed char</code>.</p> <p>All of Cocoa uses <code>BOOL</code>, as does all non-NeXT/Apple Cocoa code that I've seen. Obviously, for compatibility with existing protocols (e.g., <code>-applicationShouldTerminateAfterLastWindowClosed:</code> from <code>NSApplicationDelegate</code>), matching the already-declared type is preferable, if for no other reason than to avert a warning.</p> <p>For cleanliness/readability purposes, <code>stdbool.h</code> defines <code>bool</code> as a synonym for <code>_Bool</code>, so those of us who don't want unnecessary underscores in our code can use that.</p> <p>Three other useful notes:</p> <ul> <li><code>@encode(_Bool)</code> evaluates to <code>"B"</code>. (<code>@encode(BOOL)</code> evaluates to <code>"c"</code>, for <code>signed char</code>.)</li> <li><code>sizeof(_Bool)</code> evaluates to <code>1</code>, which follows from C99's definition that <code>_Bool</code> is only as large as necessary to hold its two possible values. (<strong>Edit:</strong> Actually, the standard says only that it must be “large enough” to hold those two values; it does not place an upper bound, and, in fact, <a href="http://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/LowLevelABI/100-32-bit_PowerPC_Function_Calling_Conventions/32bitPowerPC.html#//apple_ref/doc/uid/TP40002438-SW21" rel="noreferrer">Mac OS X on 32-bit PowerPC defines it as 4 bytes.</a> Size difference is another thing to file under possible <code>BOOL</code>-vs.-<code>bool</code> compatibility issues.)</li> <li>On that note, the only two possible values of a <code>_Bool</code> are 1 and 0. Any other values are converted to one of these on assignment, as if you had done a double-negation (<code>!!</code>) or tested inequality against 0 (<code>!= 0</code>). The only ways to get a <code>_Bool</code> with some other value are the usual magicks: Pointer aliasing and unions.</li> </ul> <p>Is there any reason not to use <code>_Bool</code>/<code>bool</code> in new code?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COObjective-C isn't a 'strict superset of C' it is a 'a strict superset of a *particular* C' and is likely closely associated with the particular Objective-C version being targeted... anyway, ramble aside: Objective-C is also about conventions and for a particular environment / set of environments (as pointed out in the question), `BOOL` *is* part of that convention. I wouldn't rock the ship but have no "proper" justification (e.g. any incompatibilities?).
      singulars
      1. This table or related slice is empty.
    2. COAlso, `sizeof(t)` returns the number of chars, not the number of bits so ... I am confused with that statement. (Assuming 8 bits/char, that's far more than a boolean value requires).
      singulars
      1. This table or related slice is empty.
    3. COIt's a strict superset of whatever C you pick: C89, C99, or the GNU extensions thereof. To the extent that compiler-specific C extensions that aren't part of Objective-C are present, they will be whether you use (that compiler's) Objective-C or (that compiler's) plain C. And yes, it's the number of bytes (not necessarily octets); you can't have an object (by the C meaning of the term) smaller than a single byte. The closest you can get is structure members defined as some number of bits long, but even then, the implementation may/will round up as it sees fit.
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload