How to print Boolean flag in NSLog?

IosObjective CCocoa Touch

Ios Problem Overview


Is there a way to print value of Boolean flag in NSLog?

Ios Solutions


Solution 1 - Ios

Here's how I do it:

BOOL flag = YES;
NSLog(flag ? @"Yes" : @"No");

?: is the ternary conditional operator of the form:

condition ? result_if_true : result_if_false

Substitute actual log strings accordingly where appropriate.

Solution 2 - Ios

%d, 0 is FALSE, 1 is TRUE.

BOOL b; 
NSLog(@"Bool value: %d",b);

or

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

On the bases of data type %@ changes as follows

For Strings you use %@
For int  you use %i
For float and double you use %f

Solution 3 - Ios

Booleans are nothing but integers only, they are just type casted values like...

typedef signed char		BOOL; 

#define YES (BOOL)1
#define NO (BOOL)0

BOOL value = YES; 
NSLog(@"Bool value: %d",value);

If output is 1,YES otherwise NO

Solution 4 - Ios

Note that in Swift, you can just do

let testBool: Bool = true
NSLog("testBool = %@", testBool.description)

This will log testBool = true

Solution 5 - Ios

While this is not a direct answer to Devang's question I believe that the below macro can be very helpful to people looking to log BOOLs. This will log out the value of the bool as well as automatically labeling it with the name of the variable.

#define LogBool(BOOLVARIABLE) NSLog(@"%s: %@",#BOOLVARIABLE, BOOLVARIABLE ? @"YES" : @"NO" )

BOOL success = NO;
LogBool(success); // Prints out 'success: NO' to the console

success = YES;
LogBool(success); // Prints out 'success: YES' to the console

Solution 6 - Ios

Apple's FixIt supplied %hhd, which correctly gave me the value of my BOOL.

Solution 7 - Ios

We can check by Four ways

The first way is

BOOL flagWayOne = TRUE; 
NSLog(@"The flagWayOne result is - %@",flagWayOne ? @"TRUE":@"FALSE");

The second way is

BOOL flagWayTwo = YES; 
NSLog(@"The flagWayTwo result is - %@",flagWayTwo ? @"YES":@"NO");

The third way is

BOOL flagWayThree = 1;
NSLog(@"The flagWayThree result is - %d",flagWayThree ? 1:0);

The fourth way is

BOOL flagWayFour = FALSE; // You can set YES or NO here.Because TRUE = YES,FALSE = NO and also 1 is equal to YES,TRUE and 0 is equal to FALSE,NO whatever you want set here.
NSLog(@"The flagWayFour result is - %s",flagWayFour ? YES:NO);
 

Solution 8 - Ios

NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership);  // prints 1 or 0

Solution 9 - Ios

Here is how you can do it:

BOOL flag = NO;
NSLog(flag ? @"YES" : @"NO");

Solution 10 - Ios

In Swift, you can simply print a boolean value and it will be displayed as true or false.

let flag = true
print(flag) //true

Solution 11 - Ios

//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));

Solution 12 - Ios

  • Direct print bool to integer
BOOL curBool = FALSE;
NSLog(@"curBool=%d", curBool);

-> curBool=0

  • Convert bool to string
char* boolToStr(bool curBool){
    return curBool ? "True": "False";
}

BOOL curBool = FALSE;
NSLog(@"curBool=%s", boolToStr(curBool));

-> curBool=False

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
QuestionDevangView Question on Stackoverflow
Solution 1 - IosBoltClockView Answer on Stackoverflow
Solution 2 - IosSashaQblView Answer on Stackoverflow
Solution 3 - IosChandan Shetty SPView Answer on Stackoverflow
Solution 4 - IosarcticmattView Answer on Stackoverflow
Solution 5 - IosxizorView Answer on Stackoverflow
Solution 6 - Iosgreen_knightView Answer on Stackoverflow
Solution 7 - Iosuser3182143View Answer on Stackoverflow
Solution 8 - IosSaqib R.View Answer on Stackoverflow
Solution 9 - IosSAQIB SOHAIL BHATTIView Answer on Stackoverflow
Solution 10 - IosTamás SengelView Answer on Stackoverflow
Solution 11 - IosZen Of KursatView Answer on Stackoverflow
Solution 12 - IoscrifanView Answer on Stackoverflow