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


当前回答

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

其他回答

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

let flag = true
print(flag) //true

你可以这样做:

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

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

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

这将记录testBool = true

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

直接将bool值打印为整数

BOOL curBool = FALSE;
NSLog(@"curBool=%d", curBool);

- > curBool = 0

将bool类型转换为字符串

char* boolToStr(bool curBool){
    return curBool ? "True": "False";
}

BOOL curBool = FALSE;
NSLog(@"curBool=%s", boolToStr(curBool));

- > curBool =虚假