简体   繁体   中英

NSUserDefault - Not Able to fetch value

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM