NSUserDefault - Not Able to fetch value

70 views Asked by At

In didSelectRowAtIndexPath Method of tableView , I am saving some data in NSUserDefault like this

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:selectedCell.textLabel.text forKey:@"tpName"];

and at another place I am fetching this data with this code ,

NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
NSLog(@"tpName is %@",[storeData valueForKey:@"tpName"]);

But In this NSLog I am getting null value . Can Anyone suggest me where am I doing wrong ?

1

There are 1 answers

5
aapierce On

The NSUserDefaults doesn't get written out to disk until it gets synchronized. The user defaults get synchronized automatically at periodic intervals, or you can call it manually like so:

UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSUserDefaults *storeData=[NSUserDefaults standardUserDefaults];
[storeData setObject:selectedCell.textLabel.text forKey:@"tpName"];
[storeData synchronize];

If you are quitting your application shortly after the user defaults get set (without synchronizing manually), your standardDefaults will not be updated on disk.