代码改变世界

NSUserDefaults保存问题

2012-12-05 15:24  Y_York  阅读(269)  评论(0编辑  收藏  举报

If you terminate your app by pressing the home button (in the Simulator or on the device), your User Defaults will get saved.

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your User Defaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

[[NSUserDefaults standardUserDefaults] synchronize];


来自 :http://stackoverflow.com/questions/2622754/why-is-nsuserdefaults-not-saving-my-values/2622940#2622940

模拟器中按stop终止程序可能会没有保存数据。对于关键数据建议手动synchronize同步一次。