How to print Boolean flag in NSLog?

后端 未结 11 2336
深忆病人
深忆病人 2020-12-02 04:06

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

相关标签:
11条回答
  • 2020-12-02 04:16

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

    0 讨论(0)
  • 2020-12-02 04:17

    Note that in Swift, you can just do

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

    This will log testBool = true

    0 讨论(0)
  • 2020-12-02 04:17

    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);
    
    0 讨论(0)
  • 2020-12-02 04:18

    Here is how you can do it:

    BOOL flag = NO;
    NSLog(flag ? @"YES" : @"NO");
    
    0 讨论(0)
  • 2020-12-02 04:31

    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.

    0 讨论(0)
  • 2020-12-02 04:31
    //assuming b is BOOL. ternary operator helps us in any language.
    NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
    
    0 讨论(0)
提交回复
热议问题