Craig
Craig

Reputation: 16339

BOOL to NSString

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?

Upvotes: 60

Views: 93834

Answers (8)

HammerSlavik
HammerSlavik

Reputation: 161

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

Upvotes: 0

Patrick Perini
Patrick Perini

Reputation: 22633

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!

Upvotes: 23

user2941395
user2941395

Reputation: 109

This is work for me:

NSLog(@"The BOOL value is %@", theBoolValue ? "YES" : "NO");

Upvotes: -1

invisible squirrel
invisible squirrel

Reputation: 3008

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

Upvotes: 33

Hot Licks
Hot Licks

Reputation: 47759

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

Upvotes: 13

Andrew Grant
Andrew Grant

Reputation: 58804

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

Upvotes: 65

Nuoji
Nuoji

Reputation: 3448

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

Upvotes: 3

user23743
user23743

Reputation:

You need a formatting specifier in your format string:

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

Upvotes: 63

Related Questions