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


当前回答

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

其他回答

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

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

我是这样做的:

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

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

condition ? result_if_true : result_if_false

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

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

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

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

这将记录testBool = true