how to print out bool in objective c

前端 未结 6 960
长发绾君心
长发绾君心 2021-02-19 15:20

I have set a bool value for key TCshow in my NSUserDefault, I want to run a nslog test whether the key is saved or not, and i m trying to printout the bool value. here is my cod

相关标签:
6条回答
  • NSLog(@"The value is %s", [self.storedKey boolForKey:@"TCshow"] ? "TRUE" : "FALSE");
    
    0 讨论(0)
  • 2021-02-19 15:55

    Just for the sake of using the new syntax you could always box the bool so that is an object and can be printed with %@

    NSLog(@"%@", @( [self.storedKey boolForKey:@"TCshow"] ));
    
    0 讨论(0)
  • 2021-02-19 15:58

    you should use

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

    here flag is your BOOL.

    0 讨论(0)
  • 2021-02-19 15:59

    %@ is for objects. BOOL is not an object. You should use %d.

    It will print out 0 for FALSE/NO and 1 for TRUE/YES.

    0 讨论(0)
  • 2021-02-19 16:00
    NSLog(@"%d", [self.storedKey boolForKey:@"TCshow"]);
    
    0 讨论(0)
  • 2021-02-19 16:02
    if([self.storedKey boolForKey:@"TCshow"]){
    NSLog(@"YES");
    }
    else{
    NSLog(@"NO");
    
    }
    

    I think it will be helpful to you.

    0 讨论(0)
提交回复
热议问题