BOOL to NSString

Craig picture Craig · Apr 10, 2009 · Viewed 91.3k times · Source

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?

Answer

Andrew Grant picture Andrew Grant · Apr 10, 2009

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