问题
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?
回答1:
You need a formatting specifier in your format string:
NSLog(@"Is Kind of NSString: %@", ([thing isKindOfClass:[NSString class]]) ? @"YES" : @"NO");
回答2:
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");
回答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
回答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!
回答5:
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));
回答6:
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");
回答7:
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]]));
回答8:
This is work for me:
NSLog(@"The BOOL value is %@", theBoolValue ? "YES" : "NO");
来源:https://stackoverflow.com/questions/738524/bool-to-nsstring