有办法打印值布尔标志在NSLog?


当前回答

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

其他回答

布尔值只是整数,它们只是类型转换的值,比如……

typedef signed char     BOOL; 

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

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

如果输出为1,则为YES,否则为NO

虽然这不是对Devang的问题的直接回答,但我相信下面的宏对寻找日志bool的人非常有帮助。这将注销bool值,并自动用变量名标记它。

#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

苹果的FixIt提供了%hhd,它正确地给了我BOOL的值。

注意,在Swift中,你可以这样做

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

这将记录testBool = true

在Swift中,你可以简单地打印一个布尔值,它将显示为true或false。

let flag = true
print(flag) //true