【问题标题】:How to print Boolean flag in NSLog?如何在 NSLog 中打印布尔标志?
【发布时间】:2011-09-15 13:06:28
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:
    • 直接将 bool 打印为整数
    BOOL curBool = FALSE;
    NSLog(@"curBool=%d", curBool);
    

    ->curBool=0

    • 将布尔转换为字符串
    char* boolToStr(bool curBool){
        return curBool ? "True": "False";
    }
    
    BOOL curBool = FALSE;
    NSLog(@"curBool=%s", boolToStr(curBool));
    

    ->curBool=False

    【讨论】:

      【解决方案2】:

      在 Swift 中,您可以简单地打印一个布尔值,它将显示为 truefalse

      let flag = true
      print(flag) //true
      

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 一个有用的宏,尤其是显示变量名的技巧。我个人不会使用 BOOL 作为参数名称以避免混淆。
        【解决方案4】:

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

        第一种方式是

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

        第二种方式是

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

        第三种方式是

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

        第四种方式是

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

        【讨论】:

          【解决方案5】:

          你可以这样做:

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

          【讨论】:

          • 这基本上是四年前@BoltClock 部分答案的重复。
          【解决方案6】:

          %d0 为假,1 为真。

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

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

          基于%@的数据类型变化如下

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

          【讨论】:

            【解决方案7】:

            我是这样做的:

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

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

            condition ? result_if_true : result_if_false
            

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

            【讨论】:

            • 也很容易将其设为宏:#define StringFromBOOL(b) ((b) ? @"YES" : @"NO")
            • 这票怎么这么多?这不是如何记录布尔值,而是如何根据布尔值记录不同的值。
            • @Acey:显然,人们(包括最初的提问者)对后者更感兴趣。如果我冒险猜测,那是因为直接打印值 (0/1) 没有多大意义。
            • @BoltClock 0/1 在日志输出中没有意义?我以为我们都是程序员,哈哈
            【解决方案8】:

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

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

            这将记录testBool = true

            【讨论】:

            • 在 Swift 中你可以使用print()
            【解决方案9】:
            NSArray *array1 = [NSArray arrayWithObjects:@"todd1", @"todd2", @"todd3", nil];
            bool objectMembership = [array1 containsObject:@"todd1"];
            NSLog(@"%d",objectMembership);  // prints 1 or 0
            

            【讨论】:

              【解决方案10】:
              //assuming b is BOOL. ternary operator helps us in any language.
              NSLog(@"result is :%@",((b==YES)?@"YES":@"NO"));
              

              【讨论】:

              • (b==YES) 与 b 相同。如所列,您依靠编译器的优化器将其降低到 (b ? @"YES" : @"NO")
              【解决方案11】:

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

              【讨论】:

                【解决方案12】:

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

                typedef signed char     BOOL; 
                
                #define YES (BOOL)1
                #define NO (BOOL)0
                
                BOOL value = YES; 
                NSLog(@"Bool value: %d",value);
                

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

                【讨论】:

                • 不,布尔值是signed char。如果提供了 0 或 1 以外的值,您的表达式可能会计算错误。
                • 不,BOOL 的类型取决于您的编译器(32 位与 64 位),并且通常与 bool 类型不同。另一方面,bool 是 bool,是标准类型,与 signed char 不同。
                猜你喜欢
                • 2012-11-11
                • 2011-10-26
                • 1970-01-01
                • 2013-12-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-07-02
                相关资源
                最近更新 更多