Cannot set NSUserDefaults field
I have the following code in my initialization (first time) of my app:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=@"1";
[defaults setObject:uid forKey:@"init_val"];
[defaults synchronize];
Much later in the code (in response to a button press) I check the value using:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaults objectForKey:@"init_val"];
initVal is always nil. I have checked and init_val is set up exactly the same in my settings bundle as another field that I can set and read from without issue (they are both set up with a single field name开发者_开发技巧d "Key".
@mlewis54:
You can try this code. I am very sure that it will work for you.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=@"1";
[defaults setObject:uid forKey:@"init_val"];
[defaults synchronize];
For Retrieving Data You Should Use The Following Code :
NSString *initVal=[[NSUserDefaults standardUserDefaults] valueForKey:@"init_val"];
OR
NSString *initVal=[[NSUserDefaults standardUserDefaults] objectForKey:@"init_val"];
EDIT:
If still it gives nil
, then you can use the following code:
NSString *initVal=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] valueForKey:@"init_val"]];
OR
NSString *initVal=[NSString stringWithFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"init_val"]];
Hope this helps you.
Decelare this at an event on click of button ...
NSUserDefaults *savedData = [NSUserDefaults standardUserDefaults];
[savedData setObject:userEmailTextField.text forKey:@"userid"];
and on load load the data and check the value you have entered is showing or not...
userEmailTextField.text =[[NSUserDefaults standardUserDefaults]objectForKey:@"userid"];
You need to use the line
NSString *initVal = [defaults stringForKey:@"init_val"];
- since it's a String you must use stringForKey
. You can similarly use boolForKey
to get a boolean value.
There's nothing wrong in your code, i tried to copy it in my project and it worked good, so your error could be probably when you check initVal...
what's the code you use to see if initVal is nil? and are you sure that when/where you check initVal, that var/oblect is still visible ?
try to insert this immediately after your reading code:
NSLog(@"____text read in pref: %@", initVal);
it writes this in my console window -> __text read in pref: 1
of course it's an NSString, i hope you keep it in mind it's not an int or a NSNumber when you check it...
luca
It might be that the initialisation isn't happening. Put an NSLog in there to check it's executed.
It works fine for me with the same code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *uid=@"1";
[defaults setObject:uid forKey:@"init_val"];
[defaults synchronize];
NSUserDefaults *defaultss = [NSUserDefaults standardUserDefaults];
NSString *initVal=[defaultss objectForKey:@"init_val"];
NSLog(@"Value of initVal: %@",initVal);
O/P on console: Value of initVal: 1
So try to do it with the same code with print on console.
Are you sure you properly initialize NSUserDefaults? When I initialize, in my app delegate, I always create the default user defaults by creating a dictionary of the default values then:
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Where appDefaults is the dictionary. Then you should be able to access them correctly.
精彩评论