Is there a format specifier that works with Boolean values?

Jay Imerman picture Jay Imerman · Jul 19, 2011 · Viewed 13.1k times · Source

I want to do something like this:

NSLog(@"You got: %x", booleanValue);

where x is the specifier. But I can't find one! I want to avoid:

if (booleanValue) {
    NSLog(@"You got: YES");
}
else {
    NSLog(@"You got: NO");
}

Any ideas? The docs didn't have a Boolean specifier. %@ didn't work either.

Answer

PengOne picture PengOne · Jul 19, 2011

Here are two things that work:

NSLog(@"You got: %@",booleanValue ? @"YES" : @"NO");

or you can cast:

NSLog(@"You got: %d", (int)booleanValue);

Which will output 0 or 1