124

What formatter is used for boolean values?

EDIT:

Example: NSLog(@" ??", BOOL_VAL);, what is ?? ?

Moshe
  • 57,511
  • 78
  • 272
  • 425

9 Answers9

178

One way to do it is to convert to strings (since there are only two possibilities, it isn't hard):

NSLog(@" %s", BOOL_VAL ? "true" : "false");

I don't think there is a format specifier for boolean values.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
72

I would recommend

NSLog(@"%@", boolValue ? @"YES" : @"NO");

because, um, BOOLs are called YES or NO in Objective-C.

Yuji
  • 34,103
  • 3
  • 70
  • 88
  • 2
    It seems an obvious utility spot for a macro or a function (if only to avoid the propagation of string literals throughout the app). – Warren P Apr 09 '10 at 13:48
  • Only you don't really need to to avoid "propagation" of such strings - as ObjC coallescs same-valued instances of immutable strings (and other objects) - such that only one copy resides in memory. Macro is OK, function is OK, and copy-and-paste are OK too. – Motti Shneor Apr 03 '19 at 11:22
58

Use the integer formatter %d, which will print either 0 or 1:

NSLog(@"%d", myBool);
Erin Geyer
  • 11,754
  • 1
  • 24
  • 24
24

In Objective-C, the BOOL type is just a signed char. From <objc/objc.h>:

typedef signed char BOOL;
#define YES         (BOOL)1
#define NO          (BOOL)0

So you can print them using the %d formatter But that will only print a 1 or a 0, not YES or NO.

Or you can just use a string, as suggested in other answers.

mipadi
  • 398,885
  • 90
  • 523
  • 479
23

Add this inline function to your .h file:

static inline NSString* NSStringFromBOOL(BOOL aBool) {
    return aBool? @"YES" : @"NO";
}

Now you are ready to go...

NSLog(@"%@", NSStringFromBOOL(BOOL_VAL));
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
gigahari
  • 671
  • 1
  • 7
  • 19
5

Format strings for use with NSLog and [NSString stringWithFormat] are documented here:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

BOOL/bool/boolean are not even mentioned...

DLRdave
  • 13,876
  • 4
  • 53
  • 70
4

I believe the easiest way to do this is:

NSLog(@" %@", @(BOOL_VAL));

@(expression)

Dynamically evaluates the boxed expression and returns the appropriate object literal based on its value (i.e. NSString for const char*, NSNumber for int, etc.).

asamoylenko
  • 2,107
  • 2
  • 18
  • 13
1

Just add the below function and pass it the BOOL value and method will return back the NSString

- (NSString *)boolValueToString:(BOOL)theBool {
    if (theBool == 0)
        return @"NO"; // can change to No, NOOOOO, etc
    else
        return @"YES"; // can change to YEAH, Yes, YESSSSS etc
}
Ramkrishna Sharma
  • 6,961
  • 3
  • 42
  • 51
Bryan Norden
  • 2,397
  • 18
  • 22
0

I created a category of NSString with this

+ (instancetype)stringWithBool:(BOOL)boolValue {
return boolValue ? @"YES" : @"NO";
}

And use it like this:

[NSString stringWithBool:boolValue];
xlsmearlx
  • 628
  • 8
  • 9