standardUserDefaults does not save boolean values - ios

I have exhausted all my resources. The very simple process of storing a boolean value in standardUserDefaults simply does not work for me. Here's my test code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"BoolKey"];
[defaults setObject:#"Hello!!!" forKey:#"StrKey"];
[defaults synchronize];
BOOL b = [defaults boolForKey:#"BoolKey"]; // b equals NO
NSString *s = [defaults stringForKey:#"StrKey"]; // s equals "Hello!!!"
I can't understand why strings are stored fine, while booleans are not. Does anyone have any idea? I'm using XCode 6.0.1 (6A317).
Update: I found that changing the KeyName actually solved my problem. My boolean key was named LoginOK. I changed it to CredentialsStored, and now it loads correctly. Weird though...

I copy pasted your code and executed
Have look to bottom right in console
BOOL returned YES

This issue has nothing to do with Xcode 6.0.1 (6A137). Your code is correct. I believe there could be an error at when you are calling or executing these commands. You should try and debug, start with a simple example and NSlog the values. Click on your iOS Simulator >> Reset Contents and Settings and try.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:true forKey:#"BoolKey"];
[defaults setObject:#"Hello!!!" forKey:#"StrKey"];
[defaults synchronize];
BOOL b = [defaults boolForKey:#"BoolKey"];
NSLog(#"%d",b);
NSString *s = [defaults stringForKey:#"StrKey"];
NSLog(#"%#",s);

Related

NSUserDefault is clearing once the application kills

For Saving the data
NSString *balSnapTokenFromResponce;
balSnapTokenFromResponce = #"456294797493749873";
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:balSnapTokenFromResponce forKey:#"Token"];
[defaults synchronize];
For Retreiving the Data
[[NSUserDefaults standardUserDefaults] stringForKey:#"Token"]
This, as shown, makes no sense:
[[NSUserDefaults standardUserDefaults] stringForKey:#"Token"]
You must do something with the return value:
NSString *balSnapTokenFromResponce = [[NSUserDefaults standardUserDefaults] stringForKey:#"Token"];
Perhaps you are, but that's not clear from your question. Because otherwise the code you've posted should work, assuming it's getting called.

Cannot setBool NSUserDefaults [duplicate]

This question already has answers here:
NSUserDefaults. setValue works, Not setBool
(4 answers)
Closed 7 years ago.
I'm trying to set userDefaults, but it isn't working. Have anyone else experience this problem?
Tried both on simulator and device. Xcode 6.3.1 and iOS 8.3
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"active"];
BOOL active = [defaults boolForKey:#"active"]; // RETURN NO
chek this code:
you missing synchronize nsuserdefault.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:#"active"];
[defaults synchronize];
BOOL active = [defaults boolForKey:#"active"];

NSUserDefaults overriding previously saved values

I have two completely separate tasks being carried out but somehow they seem to connect.
In ViewController 1, I have:
NSString *foo = #"foo";
NSUserDefaults *default1 = [NSUserDefaults standardUserDefaults]
[default1 setObject:foo forKey:#"foo"];
[default1 synchronize];
and when I do:
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"foo"]);
The value printed is what I expect it to be.
In ViewController 2, I have:
NSString *bar = #"bar";
NSUserDefaults *default2 = [NSUserDefaults standardUserDefaults]
[default2 setObject:bar forKey:#"bar"];
[default2 synchronize];
And same again when I NSLog it, the value is what I expect it to be.
But somehow when i try to print object #"foo" again it gives me the value for the second object, in this case #"bar"
Any guidance on why my original value is being overridden by the second value even tough the variable/key names are different in the 2 classes?
You are doing this for both:
[foo setObject:bar forKey:#"bar"];
Shouldn't it be:
[defaults1 setObject:foo forKey:#"foo"];
And
[defaults2 setObject:bar forKey:#"bar"];
There is however, a greater problem:
NSString *foo = #"foo";
NSUserDefaults *default1 = [NSUserDefaults standardUserDefaults];
[default1 setObject:foo forKey:#"foo"];
[default1 synchronize];
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"foo"]);
NSString *bar = #"bar";
NSUserDefaults *default2 = [NSUserDefaults standardUserDefaults];
[default2 setObject:bar forKey:#"bar"];
[default2 synchronize];
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"bar"]);
Will work properly, however you're creating defaults1 and defaults2 which are both instances of standard defaults, so:
NSUserDefaults * defaults1 = [NSUserDefaults standardUserDefaults];
NSUserDefaults * defaults2 = [NSUserDefaults standardUserDefaults];
[NSUserDefaults standardUserDefaults];
Are all pointers to the standardUserDefaults singleton. So they are identical instances. You could simply do:
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
NSString *foo = #"foo";
[standardDefaults setObject:foo forKey:#"foo"];
NSString *bar = #"bar";
[standardDefaults setObject:bar forKey:#"bar"];
[standardDefaults synchronize];
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"foo"]);
NSLog(#"%#", [[NSUserDefaults standardUserDefaults] objectForKey:#"bar"]);
You can print out the entire set of user defaults with code like this
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *dictionary = [defaults dictionaryRepresentation];
NSLog( #"%#", dictionary );
Note that Apple puts lots of garbage in the user defaults, so you'll have to scroll to the end to see foo and bar. I'm guessing that you have a typo in code that you haven't shown us, and either foo is being overwritten, or foo simply isn't being displayed properly.
The current code in your question (after 1 edit) is all good and should work correctly.

`[NSUserDefaults standardUserDefaults]` returns nil

I want to save some user preferences, but
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
just returns nil.
iOS-Developper Library says, that this should return the existing shared defaults object or create one if none exists... What am I missing?
I also use Appirater and there all this stuff seems also not to work...
This code gets called when the user pushes a button...
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int index = ([defaults integerForKey:#"BackgroundColorSpecifier"]+ 1)%self.backgroundColors.count;
[defaults setInteger:index forKey:#"BackgroundColorSpecifier"];
[defaults synchronize];
This gets called in application: didFinishLaunchingWithOptions:
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults registerDefaults:#{#"BackgroundColorSpecifier": #0}];
[standardDefaults synchronize];
When I debug this code snippets the green "position-indicator" jumps around in a very strange manner...
I don't have any clue, whats going on... Please help!
This is far more likely to be a problem with the debugger than anything else, particularly with your other issues. I've seen similar things in my own projects but don't have a reliable way of clearing it out other than the usual restart / clean options.
NSLogs will usually give more consistent results than the debugger if the debugger is having an off day.
NSUserDefaults isn't broken. We'd have heard about it by now.
you can use this function to log you userDefaults dic
- (void)logCache
{
NSDictionary * dic = [[NSBundle mainBundle] infoDictionary];
NSString *bundleId = [dic objectForKey: #"CFBundleIdentifier"];
NSUserDefaults *appUserDefaults = [[NSUserDefaults alloc] init];
NSDictionary *cacheDic = [appUserDefaults persistentDomainForName: bundleId];
NsLog(#"cacheDic::%#",cacheDic);
}

NSUserDefaults not being set

This is my code in AppDelegate.m -didFinishLaunchingWithOptions:
// set color scheme
SingletonColorScheme *colorScheme = [SingletonColorScheme sharedColorScheme]; // initialize
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(colorScheme.colorScheme == nil)
[defaults setObject:#"Saori" forKey:#"colorScheme"];
else
[defaults setObject:colorScheme.colorScheme forKey:#"colorScheme"];
[defaults synchronize]; // write them to disk
colorScheme.colorScheme = [defaults objectForKey:#"colorScheme"]; // set the singleton
NSLog(#"\n\nAppDelegate - colorScheme: %#\ndefault: %#\n\n", colorScheme.colorScheme, [[NSUserDefaults standardUserDefaults] stringForKey:#"colorScheme"]);
The NSLog shows this:
AppDelegate - colorScheme:
default: (null)
I know now that the code is not correct, but for the life of me, I don't see what's wrong. I would appreciate some help fixing this! :D
SD
UPDATED: changed key for NSLog
stringForKey:#"Saori"
should be
stringForKey:#"colorScheme"
-- you want to get the object for the key that you used previously.

Resources