Cannot setBool NSUserDefaults [duplicate] - ios

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

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.

standardUserDefaults does not save boolean values

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

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

How would I save a UIButton's properties and load with a button? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How can I save and load the alpha values of a UIButton in an app?
I would like to save the state of the UIButton (e.g. its alpha value and whether it is hidden or not) and this would then load up when the user quits and reloads the app.
I've tried some bits of code with NSUserDefaults but with no luck.
Could somebody help with some sample code so that I can save and load the button's state?
Thanks,
James
Related to Shaharyar's answer (i don't know how to comment):
in this case you need to use NSNumber.
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithFloat:SOME_FLOAT] forKey:KEY];
because float is not an object, but NSNumber is one.
EDITED:
1) To make sure your defaults are created after the application runs at first time:
in your AppDelegate's initialize-method:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:SOME_FLOAT], #"YOUR_KEY",
nil];
[defaults registerDefaults:appDefaults];
2) Updating defaults after:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat:FLOAT_VALUE forKey:#"YOUR_KEY"];
[prefs synchronize];
3) Read defaults:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float FLOAT_VALUE = [prefs floatForKey:#"YOUR_KEY"];
Can you post some of the code?
NSUserDefaults is the place to store such information..
Assumption:
Did you make a call to [NSUserDefaults synchronize] after setting the values?
Code:
// Setting a value
[[NSUserDefaults standardUserDefaults] setValue:VALUE forKey:KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
// Getting a value
NSString *var1 = [[NSUserDefaults standardUserDefaults] valueForKey:KEY];
In your case it would be:
// Setting a value
[[NSUserDefaults standardUserDefaults] setFloat:VALUE forKey:KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

Resources