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

本文翻译自:How to print Boolean flag in NSLog?

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


#1楼

参考:https://stackoom.com/question/Qg61/如何在NSLog中打印布尔标志


#2楼

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


#3楼

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. 虽然这不是对Devang问题的直接回答,但我相信下面的宏对于想要记录BOOL的人来说非常有帮助。 This will log out the value of the bool as well as automatically labeling it with the name of the variable. 这将注销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

#4楼

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

#5楼

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

#6楼

Note that in Swift, you can just do 请注意,在Swift中,您可以这样做

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

This will log testBool = true 这将记录testBool = true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值