How to print Boolean flag in NSLog?

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

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

相关标签:
11条回答
  • 2020-12-02 04:32
    NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
    bool objectMembership = [array1 containsObject:@"todd1"];
    NSLog(@"%d",objectMembership);  // prints 1 or 0
    
    0 讨论(0)
  • 2020-12-02 04:33

    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

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

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

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

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

    let flag = true
    print(flag) //true
    
    0 讨论(0)
提交回复
热议问题