BOOL to NSString

Viewed 92680

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?

8 Answers

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");

You need a formatting specifier in your format string:

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

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");
Related