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


当前回答

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

typedef signed char     BOOL; 

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

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

如果输出为1,则为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

%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
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));

我是这样做的:

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

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

condition ? result_if_true : result_if_false

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

NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership);  // prints 1 or 0