BOOL for NSString

If I have a method that returns BOOL , how do I apply it to NSString so that I can print it to the console?

For example, I tried to do this, which does not work:

 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 that this is a primitive data type, so I can not call methods on it. Do I need to create a string separately and then use Bool as a parameter in a method on NSString?

+58
objective-c boolean nsstring
Apr 10 '09 at 18:56
source share
8 answers

In the format string, you will need a formatting specifier:

 NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO"); 
+63
Apr 10 '09 at 19:03
source share

Use the ternary operator :

 BOOl isKind= [thing isKindOfClass:[NSString class]]; NSLog(@"Is Kind of NSString: %d", isKind); NSLog(@"Is Kind of NSString: %@", isKind ? @"YES" : @"NO"); 
+64
Apr 10 '09 at 19:08
source share

In the background, BOOL acts like an int type, so you can use% i to check the value of BOOL types in NSLog:

 BOOL a = YES; BOOL b = NO; NSLog(@"a is %i and b is %i", a, b); // Output: a is 1 and b is 0 
+32
Feb 23 2018-11-11T00:
source share

So, I know this is really old, but I thought that I could throw my decision in the ring. I AM:

 #define NSStringFromBOOL(aBOOL) ((aBOOL) ? @"YES" : @"NO") NSLog(@"Is Kind of NSString: %@", NSStringFromBOOL([thing isKindOfClass: [NSString class]]); 

I feel this is more consistent with some Apple macros for the string ( NSStringFromClass , NSStringFromRect , NSStringFromSelector , etc.) and is usually pretty easy to use on the fly. Just make sure this macro is available somewhere globally or is often imported!

+22
Dec 13 '11 at 20:49
source share

You print BOOL as follows:

 NSLog(@"The BOOL value is %s", theBoolValue ? "YES" : "NO"); 

Or, with the new note @ , one could do:

 NSLog(@"The BOOL value is %@", @(theBoolValue)); 
+12
Sep 11
source share

NSLog uses a simple printf style call format, and your code sample does not have the character sequence needed to embed the object.

This should work:

 NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO"); 
+3
Apr 10 '09 at 19:06
source share

First of all, you must add the %@ formatting specifier. It should look like this:

 NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO"); 

You can also extract the conversion from BOOL to NSString using the extern function, like Apple, using NSStringFromCGRect , NSStringFromClass , etc.

Create a utils file or add the following code to an existing header:

 //NSString+TypeConversion.h extern NSString *NSStringFromBOOL(BOOL aBool); 

And also add the following code to the implementation:

 //NSString+TypeConversion.m NSString *NSStringFromBOOL(BOOL aBool) { return aBool ? @"YES" : @"NO"; } 

So, now you can use this function in other places, and your code will become more clear and multiple:

 #import "NSString+TypesConversion.h" NSLog(@"Is Kind of NSString: %@", NSStringFromBOOL([thing isKindOfClass:[NSString class]])); 
0
Jun 16 '16 at 7:54 on
source share

This is the job for me:

 NSLog(@"The BOOL value is %@", theBoolValue ? "YES" : "NO"); 
-one
Aug 2 '15 at 20:28
source share



All Articles