328

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

Erik Godard
  • 5,930
  • 6
  • 30
  • 33
Devang
  • 11,258
  • 13
  • 62
  • 100

12 Answers12

520

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.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 56
    Easy to make this a macro, too: `#define StringFromBOOL(b) ((b) ? @"YES" : @"NO")` – jscs Jun 15 '11 at 18:36
  • 3
    How does this have so many votes? This is NOT how to log a boolean value, this is how to log different values depending on a boolean value. – Acey Oct 29 '14 at 02:45
  • 7
    @Acey: Apparently, people (including the original asker) were more interested in the latter. If I were to hazard a guess, it's because printing the value directly (0/1) isn't very meaningful. – BoltClock Nov 23 '14 at 06:27
  • 2
    @BoltClock 0/1 isn't meaningful in log output? I thought we were all programmers here lol – Cbas Apr 03 '16 at 21:04
326

%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
Kevin
  • 53,822
  • 15
  • 101
  • 132
SashaQbl
  • 3,326
  • 1
  • 13
  • 5
18

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

Chandan Shetty SP
  • 5,087
  • 6
  • 42
  • 63
  • 1
    No, bool is `signed char`. Your expression could potentially evaluate incorrectly if a value other than 0 or 1 is supplied. – CodaFi Jun 03 '12 at 05:30
  • No, the type of BOOL depends on your compiler (32 vs 64 bit), and is often not the same type as bool. bool, on the other hand, is bool, which is a standard type, and not the same as signed char. – gnasher729 May 06 '15 at 17:48
14

Note that in Swift, you can just do

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

This will log testBool = true

arcticmatt
  • 1,956
  • 1
  • 19
  • 36
9

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
xizor
  • 1,554
  • 2
  • 16
  • 35
  • A useful macro, especially with the trick of displaying the variable name. Personally I would not use BOOL as a parameter name to avoid confusion. – jk7 Jan 13 '16 at 17:34
7

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

green_knight
  • 1,319
  • 14
  • 26
5

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);
user3182143
  • 9,459
  • 3
  • 32
  • 39
2
NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership);  // prints 1 or 0
Saqib R.
  • 2,919
  • 2
  • 26
  • 21
1

Here is how you can do it:

BOOL flag = NO;
NSLog(flag ? @"YES" : @"NO");
josliber
  • 43,891
  • 12
  • 98
  • 133
  • This is basically a repeat of part of @BoltClock 's answer from four years ago. – jk7 Jan 13 '16 at 17:31
1

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

let flag = true
print(flag) //true
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
0
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
Zen Of Kursat
  • 2,672
  • 1
  • 31
  • 47
  • (b==YES) is the same as just b. as listed, you are relying on the compiler's optimizer to reduce it back down to (b ? @"YES" : @"NO") – Armand Mar 04 '14 at 06:33
0
  • 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

crifan
  • 12,947
  • 1
  • 71
  • 56