1

It may be a small issue. or I might have done something wrong but couldn't track it. please help.

I have created an application and have stored the userToken and few flags in NSUserDefaults. The app was working fine till yesterday. but today when I opened the app for further development, all the stuff stored in NSUserDefaults are suddenly coming null. I didn't change anything. and also installed the app again to resave the data. But nothing. I am lost why this is happening.

I am on iOS 9.3.4 with Xcode Version 7.3.1 (7D1014)

here's my code

in ViewCongtroller1.m :

 NSUserDefaults *def = [NSUserDefaults  standardUserDefaults];
[def setValue:@"user1" forKey:@"Usertoken"];
[def synchronize];

and in ViewController2.h :

NSUserDefaults *def ;

in ViewController2.m :

def = [NSUserDefaults standardUserDefaults];
NSLog(@"Token = %@",[def valueForKey:@"Usertoken"]);

and here's the response

Token = (null)

Please help me guys. Thanks in Advance,

Mujib Saiyyed
  • 158
  • 10
  • When you are setting the value check that your json contains value for key `token`. – Nirav D Aug 20 '16 at 09:43
  • @NDoc Yes it has.. also other flags i am saving to nsuserdefaults are also showing null when i try to fetch them. – Mujib Saiyyed Aug 20 '16 at 09:45
  • 1
    Stop using `valueForKey:` / `setValue:forKey:` unless you know exactly what the methods do and you really need that functionality. The dedicated methods to get/set an object in user defaults is `objectForKey:` / `setObject:forKey:` – vadian Aug 20 '16 at 09:48
  • Not sure if this can be helpful, but you can refer http://stackoverflow.com/a/12001704/5012384 – Milan Gupta Aug 20 '16 at 10:34
  • If something "suddenly" stops working then it's a great idea to check over the last few commits you've made and see if anything related is in there. The git bisect tool can help you narrow down the exact commit that broke things. If you're not using source control, you just found an excellent reason to start :) – jrturton Aug 20 '16 at 11:28
  • You should un-delete and accept your answer, since it's actually the answer and everyone else is just making wild guesses, too – jrturton Aug 20 '16 at 11:29

5 Answers5

1

I sorted it out guys. There wasnt any issue with NSUserDefaults. Issue was that after assigning values NSUserDefaults, i was clearing the NSUserDefaults somewhere in my app. thanks for supporting anyway friends :)

Mujib Saiyyed
  • 158
  • 10
0

I think no any mistake in your code. but still just change setValue to setObject and then check.

[def setValue:[json valueForKey:@"token"] forKey:@"Usertoken"]; 

to

[def setObject:[json valueForKey:@"token"] forKey:@"Usertoken"];

and also check NSLog(@"%@",[json valueForKey:@"token"]); value availabe or not.

Keyur Hirani
  • 1,607
  • 14
  • 22
0

please check your json does it contain key @"token" and if yes then check is there any data in it or null

Er. Khatri
  • 1,384
  • 11
  • 29
0

If the value of [json valueForKey:@"token"] is object (any kind of object like string or else) then you should use,

 [def setObject:[json valueForKey:@"token"] forKey:@"Usertoken"];

instead of

 [def setValue:[json valueForKey:@"token"] forKey:@"Usertoken"];

And you can retrieve your data like,

 NSLog(@"Token = %@",[def objectForKey:@"Usertoken"]);
Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
0
*I used the following code to print out the location of my NSUserDefaults file.Some time we cleanup the docDirectory data in app,so its remove NSUserDefault File and we got a nil value ,

try this for debug..

NSArray *path = NSSearchPathForDirectoriesInDomains(
   NSLibraryDirectory, NSUserDomainMask, YES);
NSString *folder = [path objectAtIndex:0];
NSLog(@"Your NSUserDefaults are stored in this folder: %@/Preferences", folder);*