How to print Boolean flag in NSLog?

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

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

11条回答
  •  情话喂你
    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
    

提交回复
热议问题