ios 如何在 NSLog 中打印布尔标志?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6358349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:22:34  来源:igfitidea点击:

How to print Boolean flag in NSLog?

iosobjective-ccocoa-touch

提问by Devang

Is there a way to print value of Boolean flag in NSLog?

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

回答by BoltClock

Here's how I do it:

这是我的方法:

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

?:is the ternary conditional operator of the form:

?:是以下形式的三元条件运算符:

condition ? result_if_true : result_if_false

Substitute actual log strings accordingly where appropriate.

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

回答by SashaQbl

%d, 0is FALSE, 1is TRUE.

%d, 0为假,1为真。

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

or

或者

NSLog(@"bool %s", b ? "true" : "false");

On the bases of data type %@changes as follows

根据数据类型%@变化如下

For Strings you use %@
For int  you use %i
For float and double you use %f

回答by Chandan Shetty SP

Booleans are nothing but integers only, they are just type casted values like...

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

typedef signed char     BOOL; 

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

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

If output is 1,YES otherwise NO

如果输出为 1,则为 YES 否则为 NO

回答by arcticmatt

Note that in Swift, you can just do

请注意,在 Swift 中,您可以这样做

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

This will log testBool = true

这将记录 testBool = true

回答by xizor

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. This will log out the value of the bool as well as automatically labeling it with the name of the variable.

虽然这不是 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

回答by green_knight

Apple's FixIt supplied %hhd, which correctly gave me the value of my BOOL.

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

回答by user3182143

We can check by Four ways

我们可以通过四种方式检查

The first way is

第一种方式是

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

The second way is

第二种方式是

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

The third way is

第三种方式是

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

The fourth way is

第四种方式是

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);

回答by Tamás Sengel

In Swift, you can simply print a boolean value and it will be displayed as trueor false.

在 Swift 中,你可以简单地打印一个布尔值,它会显示为trueor false

let flag = true
print(flag) //true

回答by Saqib R.

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

回答by SAQIB SOHAIL BHATTI

Here is how you can do it:

您可以这样做:

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