本文翻译自:How to print Boolean flag in NSLog?
有没有办法在NSLog中打印布尔标志的值?
#1楼
参考:https://stackoom.com/question/Qg61/如何在NSLog中打印布尔标志
#2楼
Apple的FixIt提供了%hhd,这正确地给了我BOOL的价值。
#3楼
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. 虽然这不是对Devang问题的直接回答,但我相信下面的宏对于想要记录BOOL的人来说非常有帮助。 This will log out the value of the bool as well as automatically labeling it with the name of the variable. 这将注销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
#4楼
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
#5楼
NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership); // prints 1 or 0
#6楼
Note that in Swift, you can just do 请注意,在Swift中,您可以这样做
let testBool: Bool = true
NSLog("testBool = %@", testBool.description)
This will log testBool = true
这将记录testBool = true