How to save a BOOL in iOS app

2019-08-21 17:19发布

问题:

I used the following code to save a BOOL but it's not working.

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"save"];
StrongMAN = [[NSUserDefaults standardUserDefaults] boolForKey:@"save"];

Are there other ways to save a BOOL?

回答1:

You should add the following code

[[NSUserDefaults standardUserDefaults] synchronize];


回答2:

[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"kLogIn"];
[[NSUserDefaults standardUserDefaults] synchronize];


回答3:

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"testingBool"];
BOOL test = [[NSUserDefaults standardUserDefaults] boolForKey:@"testingBool"];