Is there a format specifier that works with Boolean values?

淺唱寂寞╮ 提交于 2019-12-03 10:11:46
PengOne

Here are two things that work:

NSLog(@"You got: %@",booleanValue ? @"YES" : @"NO");

or you can cast:

NSLog(@"You got: %d", (int)booleanValue);

Which will output 0 or 1

You can cast it to an int and use %d:

NSLog(@"You got: %d", (int)booleanValue);

Or use something like this:

NSLog(@"You got: %@", booleanValue ? @"YES" : @"NO");

There's no format specifier that I know of. You can do this:

NSLog(@"You got: %@", (booleanValue ? @"YES" : @"NO"));

Alternately, you could write a little function or macro using the logic above that takes a BOOL and returns the appropriate string. You can then use that function in your log statements.

ViruMax

Yes

Here is the code:

NSLog(@"%hhd",BOOLvariable);

Prints 1 for Yes and 0 for No. Worked for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!