views:

650

answers:

4

What formatter is used for boolean values?

EDIT:

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

+3  A: 

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
Shouldn't the output strings be: "Yes" and "No" :P
Ben S
Or "Cake" and "No cake". Whatever suits you.
Michael Myers
@mmyers: I love cake and no cake! I am going to replace all my x?@"YES":@"NO" code with x?@"Cake":@"No Cake" immediately :D -- well, at least for my object descriptions anyway ;)
Jason Coco
Or TheCakeIsALie and TheCakeIsNotALie.
Warren P
+2  A: 

I would recommend

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

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

Yuji
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
+1  A: 

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
Won't %c try to print the ASCII character 0 or 1 both of which are control characters?
JeremyP
Indeed. `%d` should be used.
mipadi
A: 

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