Unexpected results from NSUserDefaults boolForKey

After completely removing the application from the device, and then downloading it to the debugger, I am trying to establish a way to load the flag using boolForKey. The first time I run the application, I have the expectation that bool will not exist, since I just reinstalled the application. I expect from the documentation that boolForKey will return NO.

I see the opposite. boolForKey returns YES, which confuses my initial user settings. Any idea why this could be happening or something like that?

BOOL stopAutoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"StopAutoLogin"]; _userWantsAutoLogin = !stopAutoLogin; 

So, stopAutoLogin comes out as "YES", which is completely unexpected.

Stranger and stranger: When I call objectForKey: @ "StopAutoLogin", I get a null object, as expected. This is just a boolForKey that returns bad value. So I changed the code to this:

 // this is nil NSObject *wrapper = [[NSUserDefaults standardUserDefaults] objectForKey:@"StopAutoLogin"]; // this is YES BOOL stopAutoLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"StopAutoLogin"]; 
+4
source share
2 answers

try [UserDefaults synchronize];

Since this method is automatically called at periodic intervals, use this method only if you cannot wait for automatic synchronization (for example, if your application is about to exit) or if you want to update the user's default disk settings, even if you have not made any changes.

see below: http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

+1
source

Are you registering default values โ€‹โ€‹for your keys?

 NSMutableDictionary *appDefaults = [NSMutableDictionary dictionaryWithCapacity:1]; [appDefaults setObject:@"NO" forKey:kReloadOnStartKey]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults registerDefaults:appDefaults]; 

If there is no registration domain, one is created using the specified dictionary and NSRegistrationDomain is added to the end of the search list.

The contents of the registration domain is not written to disk; You need to call this method every time your application starts. You can place the plist file in the Resource Catalog application and call registerDefaults: with the contents that you read from this file.

See the link for more details.

0
source

All Articles