BOOL to NSString
Solution 1:
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:
You need a formatting specifier in your format string:
NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");
Solution 3:
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:
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!