Objective-C formatting string for boolean?

Objective CStringNsstringString Formatting

Objective C Problem Overview


What formatter is used for boolean values?

EDIT:

Example: NSLog(@" ??", BOOL_VAL);, what is ?? ?

Objective C Solutions


Solution 1 - Objective C

One way to do it is to convert to strings (since there are only two possibilities, it isn't hard):

NSLog(@" %s", BOOL_VAL ? "true" : "false");

I don't think there is a format specifier for boolean values.

Solution 2 - Objective C

I would recommend

NSLog(@"%@", boolValue ? @"YES" : @"NO");

because, um, BOOLs are called YES or NO in Objective-C.

Solution 3 - Objective C

Use the integer formatter %d, which will print either 0 or 1:

NSLog(@"%d", myBool);

Solution 4 - Objective C

In Objective-C, the BOOL type is just a signed char. From <objc/objc.h>:

typedef signed char BOOL;
#define YES         (BOOL)1
#define NO          (BOOL)0

So you can print them using the %d formatter But that will only print a 1 or a 0, not YES or NO.

Or you can just use a string, as suggested in other answers.

Solution 5 - Objective C

Add this inline function to your .h file:

static inline NSString* NSStringFromBOOL(BOOL aBool) {
    return aBool? @"YES" : @"NO";
}

Now you are ready to go...

NSLog(@"%@", NSStringFromBOOL(BOOL_VAL));

Solution 6 - Objective C

Format strings for use with NSLog and [NSString stringWithFormat] are documented here:

http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html

BOOL/bool/boolean are not even mentioned...

Solution 7 - Objective C

I believe the easiest way to do this is:

NSLog(@" %@", @(BOOL_VAL));

> @(expression)

Dynamically evaluates the boxed expression and returns the appropriate object literal based on its value (i.e. NSString for const char*, NSNumber for int, etc.).

Solution 8 - Objective C

Just add the below function and pass it the BOOL value and method will return back the NSString

- (NSString *)boolValueToString:(BOOL)theBool {
    if (theBool == 0)
        return @"NO"; // can change to No, NOOOOO, etc
    else
        return @"YES"; // can change to YEAH, Yes, YESSSSS etc
}

Solution 9 - Objective C

I created a category of NSString with this

+ (instancetype)stringWithBool:(BOOL)boolValue {
return boolValue ? @"YES" : @"NO";
}

And use it like this:

[NSString stringWithBool:boolValue];

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMosheView Question on Stackoverflow
Solution 1 - Objective CMichael MyersView Answer on Stackoverflow
Solution 2 - Objective CYujiView Answer on Stackoverflow
Solution 3 - Objective CErin GeyerView Answer on Stackoverflow
Solution 4 - Objective CmipadiView Answer on Stackoverflow
Solution 5 - Objective CgigahariView Answer on Stackoverflow
Solution 6 - Objective CDLRdaveView Answer on Stackoverflow
Solution 7 - Objective CasamoylenkoView Answer on Stackoverflow
Solution 8 - Objective CBryan NordenView Answer on Stackoverflow
Solution 9 - Objective CxlsmearlxView Answer on Stackoverflow