简体   繁体   English

NSUserDefaults没有保存在模拟器中?

[英]NSUserDefaults not being saved in simulator?

My [NSUserDefaults standardUserDefaults] are not being saved. 我的[NSUserDefaults standardUserDefaults]未被保存。 Everything I close the app and start it again, it reverts back. 我关闭应用程序并重新启动它的所有内容都会恢复原状。 I don't have my iOS device handy so I am not sure if this will happen on device, but none of the other apps are doing this in simulator which leads me to believe there's something wrong with my code. 我没有我的iOS设备,所以我不确定这是否会在设备上发生,但其他应用程序都没有在模拟器中这样做,这让我相信我的代码有问题。 I don't know what part of code I should include here, it's just simple add/modify keys in nsuserdefaults and as i said it runs fine during the app, but not after i restart it... 我不知道我应该在这里包含哪些代码部分,它只是简单地在nsuserdefaults中添加/修改密钥,并且正如我所说它在应用程序中运行正常,但在我重新启动它之后却没有...

I know I can call synchronize but Apple advices against it and says I should only call it if it's a must..so. 我知道我可以调用synchronize但Apple建议反对它,并说如果它是必须的话,我应该只调用它。所以。

What could be going wrong? 怎么可能出错? Thanks. 谢谢。

Your process is possibly terminated improperly so that NSUserDefaults do not have a chance to be stored. 您的进程可能未正确终止,因此NSUserDefaults没有机会存储。 See also this and mostly this . 另见这个 ,主要是这个

The suggestion in the second post I link to is to call synchronize in applicationDidEnterBackground: 我链接到的第二篇文章中的建议是在applicationDidEnterBackground:调用synchronize applicationDidEnterBackground:

Keep also in mind that terminating your app by stopping it in Xcode most often does not save user defaults. 还要记住,通过在Xcode中停止来终止您的应用程序通常不会保存用户默认值。

Are you restarting from Xcode / debugger? 你是从Xcode /调试器重新启动? Try sending the app to background with the Home button first. 尝试首先使用“主页”按钮将应用程序发送到后台。 I think the framework synchronizes automatically then. 我认为框架会自动同步。

I could not understand about the way, you are accessing the NSUserDefault , The static function you used sharedDefaults with NSUserDefault does'nt exist in Apple Documentation 我无法理解你正在访问NSUserDefault的方式,你在Apple文档中不存在与NSUserDefault一起使用的sharedDefaults的静态函数

Use As below to access NSUserDefault 使用如下方法访问NSUserDefault

[NSUserDefaults standardUserDefaults];

For more read the blog post for using of NSUserDefault . 有关更多信息,请阅读使用NSUserDefault的博客文章。

iPhone Programming Tutorial – Saving/Retrieving Data Using NSUserDefaults iPhone编程教程 - 使用NSUserDefaults保存/检索数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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