views:

6265

answers:

3
+3  Q: 

BOOL to NSString

If I have a method that returns a BOOL, how do I cast that to a NSString so I can print it out with an NSLog?

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 a 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?

+7  A: 

You need a formatting specifier in your format string:

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

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");
Nuoji
+7  A: 
BOOl isKind= [thing isKindOfClass:[NSString class]];

NSLog("Is Kind of NSString: %d", someBool);
NSLog("Is Kind of NSString: %@", someBool ? @"YES" : @"NO");
Andrew Grant
You are missing '@' in front of those strings.
willc2