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


当前回答

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

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

这将记录testBool = true

其他回答

NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
bool objectMembership = [array1 containsObject:@"todd1"];
NSLog(@"%d",objectMembership);  // prints 1 or 0
//assuming b is BOOL. ternary operator helps us in any language.
NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));

我们可以用四种方法检查

第一种方法是

BOOL flagWayOne = TRUE; 
NSLog(@"The flagWayOne result is - %@",flagWayOne ? @"TRUE":@"FALSE");

第二种方法是

BOOL flagWayTwo = YES; 
NSLog(@"The flagWayTwo result is - %@",flagWayTwo ? @"YES":@"NO");

第三种方法是

BOOL flagWayThree = 1;
NSLog(@"The flagWayThree result is - %d",flagWayThree ? 1:0);

第四种方法是

BOOL flagWayFour = FALSE; // You can set YES or NO here.Because TRUE = YES,FALSE = NO and also 1 is equal to YES,TRUE and 0 is equal to FALSE,NO whatever you want set here.
NSLog(@"The flagWayFour result is - %s",flagWayFour ? 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的值。