【问题标题】:how to print out bool in objective c如何在目标c中打印出bool
【发布时间】:2012-08-10 18:21:42
【问题描述】:

我在我的 NSUserDefault 中为键 TCshow 设置了一个布尔值, 我想运行 nslog 测试是否保存了密钥,并且我正在尝试打印出 bool 值。 这是我的代码,但它不起作用,有什么建议吗?

- (IBAction)acceptAction:(id)sender {
//key store to nsuserdefault
self.storedKey = [[NSUserDefaults alloc] init];
[self.storedKey setBool:YES forKey:@"TCshow"];
//trying to print out yes or not, but not working...
NSLog(@"%@", [self.storedKey boolForKey:@"TCshow"]);

}

【问题讨论】:

  • 顺便说一句,这种方法适用于“条款和条件”scollview tho。我想要的是..如果新用户第一次使用该应用程序,所以推广T&C让他接受,如果他接受,那么值YES将保存到nsuserdefault键TCshow,下次他回来时,不再会为他弹出T&C视图,直接进入登录视图。 thx 如果有人可以帮助我解决这个逻辑,我会非常感激......再次感谢

标签: iphone objective-c printing boolean


【解决方案1】:

已经在another post回复,复制到这里:


  • 直接将 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】:

    你应该使用

    NSLog(flag ? @"Yes" : @"No");
    

    这里flag 是你的BOOL

    【讨论】:

      【解决方案3】:
      NSLog(@"The value is %s", [self.storedKey boolForKey:@"TCshow"] ? "TRUE" : "FALSE");
      

      【讨论】:

        【解决方案4】:

        只是为了使用新语法,您总是可以将 bool 装箱,使其成为一个对象并且可以使用 %@ 打印

        NSLog(@"%@", @( [self.storedKey boolForKey:@"TCshow"] ));
        

        【讨论】:

          【解决方案5】:
          if([self.storedKey boolForKey:@"TCshow"]){
          NSLog(@"YES");
          }
          else{
          NSLog(@"NO");
          
          }
          

          我认为这对你会有帮助。

          【讨论】:

            【解决方案6】:

            %@ 用于对象。 BOOL 不是对象。你应该使用%d

            它会打印出 0 表示 FALSE/NO 和 1 表示 TRUE/YES。

            【讨论】:

            • %d 会起作用,但请记住 BOOLsigned char 而不是 int
            【解决方案7】:
            NSLog(@"%d", [self.storedKey boolForKey:@"TCshow"]);
            

            【讨论】:

              猜你喜欢
              • 2012-12-18
              • 1970-01-01
              • 1970-01-01
              • 2011-01-15
              • 2014-01-18
              • 1970-01-01
              • 2012-05-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多