i have been working on a app, and needed to store a string. I used this code to set the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:CertificateKey.stringValue forKey:@"SavedKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(@"%@",[defaults objectForKey:@"SavedKey"]);
I loged it, so i know it saved…well, it showed me the value.
When i open my application, I use this to retrieve the default:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[CertificateKey setStringValue:[defaults objectForKey:@"SavedKey"]];
[CertificateKey setTitleWithMnemonic:[defaults objectForKey:@"SavedKey"]];
[[NSUserDefaults standardUserDefaults] synchronize];
Why will it not get the default value? Did i not completely save it?
Don’t quit the application by pressing the stop button in xcode. Instead, quit it by right clicking on the application icon and selecting “Quit”.
Edit
Maybe the first time that you execute the application, you want to save some defaults but you don’t want to set them the second+ time that the application runs.
For this purpose in some class initialize method register the defaults, like this:
Also, you’re using setValue:forKey: , that method is inherited from NSObject. Use setObject:forKey: .
And use finalize if you want to save the defaults at the end: