5

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 named "Key".

mlewis54
  • 2,372
  • 6
  • 36
  • 58
  • Hm, odd. Does the same thing happen if you use -stringForKey: instead of -objectForKey:? Also, does -synchronize return YES? – macserv Jan 31 '11 at 20:46
  • I tried stringForKey first. And synchronize returns YES – mlewis54 Jan 31 '11 at 20:52
  • Refer to my answer. I am sure you will get your solution in my answer. Do leave a comment if you face any problems with my answer. – Parth Bhatt Mar 18 '11 at 19:35

7 Answers7

5

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

Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • Thanks for the answer. Eventually I got it working this way. At the end of the day it turned out to be moot. I moved the settings functionality into the app and no longer use the settings bundle or the defaults. I appreciate everyone's input. – mlewis54 Mar 19 '11 at 13:02
1

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.

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
1

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"];
Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
breakfreehg
  • 618
  • 1
  • 6
  • 16
0

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

meronix
  • 6,175
  • 1
  • 23
  • 36
  • I check initVal using if (!initVal) { <<< then not inited >>> }. I will post debugger value when I get back to desk. I believe that it is 0x0. There is no scope issue since I am executing the statements in a row (the 2 above where I get the defaults and then the if statement). – mlewis54 Feb 01 '11 at 21:23
  • then i assure you that the code is correct, i tried this: NSString *initVal=[defaults objectForKey:@"init_val"]; if (!initVal){ NSLog(@"__1__text read in pref: %@", initVal); }else { NSLog(@"__2__text read in pref: %@", initVal); //this is executed } in console window: __2__text read in pref: 1 then a bug should be elsewhere... try to "clean all targets" under the "build" menu in xcode... does it happens in device or in simulator? – meronix Feb 01 '11 at 21:39
0

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.

Suresh Varma
  • 9,750
  • 1
  • 60
  • 91
Aaron
  • 252
  • 3
  • 16
0

It might be that the initialisation isn't happening. Put an NSLog in there to check it's executed.

joerick
  • 16,078
  • 4
  • 53
  • 57
0

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.

Ajay Sharma
  • 4,509
  • 3
  • 32
  • 59