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


当前回答

虽然这不是对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

其他回答

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

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

这将记录testBool = true

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

let flag = true
print(flag) //true

我是这样做的:

BOOL flag = YES;
NSLog(flag ? @"Yes" : @"No");

?:是三元条件运算符,形式为:

condition ? result_if_true : result_if_false

在适当的地方替换实际的日志字符串。

虽然这不是对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

%d, 0为假,1为真。

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

or

NSLog(@"bool %s", b ? "true" : "false");

基于数据类型%@的变化如下

For Strings you use %@
For int  you use %i
For float and double you use %f