BOOL
suggest changeThe BOOL type is used for boolean values in Objective-C. It has two values, YES and NO, in contrast to the more common “true” and “false”.
BOOL areEqual = (1 == 1); // areEqual is YES
BOOL areNotEqual = !areEqual; // areNotEqual is NO
NSCAssert(areEqual, @"Mathematics is a lie"); // Assertion passes
BOOL shouldFlatterReader = YES;
if (shouldFlatterReader) {
NSLog(@"Only the very smartest programmers read this kind of material.");
}
A BOOL is a primitive, and so it cannot be stored directly in a Foundation collection. It must be wrapped in an NSNumber. Clang provides special syntax for this:
NSNumber * yes = @YES; // Equivalent to [NSNumber numberWithBool:YES]
NSNumber * no = @NO; // Equivalent to [NSNumber numberWithBool:NO]
On 64-bit iOS and on watchOS, BOOL is a typedef of the C99 type bool. On other Apple platforms it is a typedef of signed char, so it is not identical to C _Bool. When Clang’s objc_bool feature is available, YES and NO are defined as the builtins __objc_yes and __objc_no, which are translated to (BOOL)1 and (BOOL)0. Otherwise YES and NO are defined directly as those casts. The definitions are in the Objective-C runtime header objc.h.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents