Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print out bool in objective c

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

}
like image 258
sefirosu Avatar asked Aug 14 '12 12:08

sefirosu


People also ask

Can you print a bool?

Normally a bool is printed as either a 0 or a 1 by std::cout , but more often than not, if you're printing a bool , it's better to see true/false .

How do you print true and false in C++?

As in C++ true refers to 1 and false refers to 0. In case, you want to print false instead of 0,then you have to sets the boolalpha format flag for the str stream. When the boolalpha format flag is set, bool values are inserted/extracted by their textual representation: either true or false, instead of integral values.


2 Answers

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

like image 117
Peter Warbo Avatar answered Sep 21 '22 17:09

Peter Warbo


you should use

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

here flag is your BOOL.

like image 36
Ankur Avatar answered Sep 21 '22 17:09

Ankur