If I have a method that returns a BOOL
, how do I cast that to an NSString
so I can print it out in console?
For example, I tried doing this, which isn't working:
NSLog(@"Is Kind of NSString:", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");
But I really want to actually turn the return value into an NSString. I know it's a primitive data type, so I can't call methods on it. Do I have to create a string separately and then use the Bool as a parameter in a method on NSString?
There is no format specifier to print boolean type using NSLog. One way to print boolean value is to convert it to a string. Another way to print boolean value is to cast it to integer, achieving a binary output (1=yes, 0=no).
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.
Use a ternary operator:
BOOl isKind= [thing isKindOfClass:[NSString class]]; NSLog(@"Is Kind of NSString: %d", isKind); NSLog(@"Is Kind of NSString: %@", isKind ? @"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