BOOL to NSString
Objective CNsstringBooleanObjective C Problem Overview
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?
Objective C Solutions
Solution 1 - Objective C
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");
Solution 2 - Objective C
You need a formatting specifier in your format string:
NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");
Solution 3 - Objective C
In the background BOOL acts like an int type so you can use %i to test for a BOOL type’s value 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
Solution 4 - Objective C
So, I know that this is really old, but I thought I might as well toss my solution into the ring. I do:
#define NSStringFromBOOL(aBOOL) ((aBOOL) ? @"YES" : @"NO")
NSLog(@"Is Kind of NSString: %@", NSStringFromBOOL([thing isKindOfClass: [NSString class]]);
I feel that this is more in line with some of Apple's to-string macros (NSStringFromClass
, NSStringFromRect
, NSStringFromSelector
, and so on), and generally pretty simple to use on-the-fly. Just be sure to put that macro somewhere globally accessible, or frequently imported!
Solution 5 - Objective C
You print a BOOL like this:
NSLog(@"The BOOL value is %s", theBoolValue ? "YES" : "NO");
Or, with the new @
notation, one could do:
NSLog(@"The BOOL value is %@", @(theBoolValue));
Solution 6 - Objective C
NSLog uses a simple printf-style invocation format its text, and your code example is missing the character sequence needed to embed an object.
This should work:
NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");
Solution 7 - Objective C
First of all you should add a formatting specifier %@
.
It should look like this:
NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");
Also you can extract a conversion from BOOL
to NSString
with extern function as Apple did with NSStringFromCGRect
, NSStringFromClass
etc.
Create utils file or add to existing ones header the following code:
//NSString+TypeConversion.h
extern NSString *NSStringFromBOOL(BOOL aBool);
And also add the following code into 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 become more clear and reusable:
#import "NSString+TypesConversion.h"
NSLog(@"Is Kind of NSString: %@", NSStringFromBOOL([thing isKindOfClass:[NSString class]]));
Solution 8 - Objective C
This is work for me:
NSLog(@"The BOOL value is %@", theBoolValue ? "YES" : "NO");