Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BOOL to NSString

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?

like image 669
Craig Avatar asked Apr 10 '09 18:04

Craig


People also ask

How to print bool value in NSLog?

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).

How do I print a Boolean value?

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.


1 Answers

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"); 
like image 56
Andrew Grant Avatar answered Oct 19 '22 06:10

Andrew Grant