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.
In C, there is no format specifier for Boolean datatype (bool). We can print its values by using some of the existing format specifiers for printing like %d, %i, %s, etc.
Format Boolean values The string representation of a Boolean is either "True" for a true value or "False" for a false value. The string representation of a Boolean value is defined by the read-only TrueString and FalseString fields.
The println(boolean) method of PrintStream Class in Java is used to print the specified boolean value on the stream and then break the line. This boolean value is taken as a parameter. Parameters: This method accepts a mandatory parameter booleanValue which is the boolean value to be written on the stream.
C does not have boolean data types, and normally uses integers for boolean testing. Zero is used to represent false, and One is used to represent true.
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
You can cast it to an int and use %d
:
NSLog(@"You got: %d", (int)booleanValue);
Or use something like this:
NSLog(@"You got: %@", booleanValue ? @"YES" : @"NO");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With