how to print out bool in objective c

18,458

Solution 1

%@ 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.

Solution 2

you should use

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

here flag is your BOOL.

Solution 3

NSLog(@"The value is %s", [self.storedKey boolForKey:@"TCshow"] ? "TRUE" : "FALSE");

Solution 4

NSLog(@"%d", [self.storedKey boolForKey:@"TCshow"]);
Share:
18,458
sefirosu
Author by

sefirosu

...not much to say for now...

Updated on June 27, 2022

Comments

  • sefirosu
    sefirosu almost 2 years

    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 code but it s not working, any suggestions?

    - (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"]);
    
    }
    
  • Ankur
    Ankur almost 12 years
    %d will work but remember BOOL is signed char not int