Xcode 9模拟器不会保存用户默认设置 [英] Xcode 9 simulator doesn't save userdefaults

查看:56
本文介绍了Xcode 9模拟器不会保存用户默认设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许是与这个问题相同.但是,我尝试过的解决方案中很少有行不通的.

Maybe it's the same issue as this one. But few of the solutions that I have tried don't work.

模拟器不会在Xcode 9.0 beta 6(9M214v)上保存用户默认设置.我也有Xcode 8(连同9),但是将其删除了.

The simulator don't save userdefaults on Xcode Version 9.0 beta 6 (9M214v). I had Xcode 8 as well (together with 9) but removed it.

我正在使用的代码:

UserDefaults.standard.setValue("1234567", forKey: "phone")
if let phone = UserDefaults.standard.value(forKey: "phone") as? String{
  //some code here
}

我还尝试过:

UserDefaults.standard.set("1234567", forKey: "phone")
if let phone = UserDefaults.standard.object(forKey: "phone") as? String{
  //some code here
} 

推荐答案

关于 synchronize 的功能以及如何编写用户默认值存在许多误解.在猜测其工作方式之前,请始终查看当前维护者的评论.他对事情很清楚.他的与git相似也很有用,请参阅他关于

There are a lot of misunderstandings about what synchronize does and how user defaults are written. Before guessing at how things work, always check the current maintainer's commentary about it. He's pretty clear about things. His analogy to git is also somewhat useful, and see his notes about iOS 8 changes. NSUserDefaults changes way more than you would think it would for such an old piece of tech.

总之,就是在生产应用程序中几乎不需要调用 synchronize .(实际上,关于弃用和删除它的讨论经常出现.)正如文档所述:

The short and long of it is that calling synchronize is almost never needed in a production app. (In fact, there's recurring discussion about deprecating and removing it.) As the docs note:

因为此方法会定期自动调用,所以仅当您无法等待自动同步(例如,如果您的应用程序即将退出)或您想要将用户默认值更新为当前值时,才使用此方法.磁盘,即使您没有进行任何更改.

Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.

甚至那些案例都与macOS有关.

And even those cases mostly relate to macOS.

对于在iOS 9.3和更高版本/macOS Sierra和更高版本上运行的应用程序,即使在这种情况下[进程间同步],也不需要-synchronize(或建议),因为默认值的键值观察现在可以在进程之间进行,因此阅读过程可以直接观察价值的变化.因此,运行在这些操作系统上的应用程序通常不应调用同步.

For applications running on iOS 9.3 and later / macOS Sierra and later, -synchronize is not needed (or recommended) even in this situation [synchronizing between processes], since Key-Value Observation of defaults works between processes now, so the reading process can just watch directly for the value to change. As a result of that, applications running on those operating systems should generally never call synchronize.

但是.....

人们在模拟器中使用用户默认设置时通常遇到的问题是,他们会在写操作之前将应用程序杀死(击中停止,重新启动应用程序,崩溃).在生产中,这通常不是问题.如果您在写用户默认值时就崩溃了,那么很可能您处于定义不佳的状态,因此无论您写垃圾,写好东西还是不写东西,都是一团糟-您想要的东西.但是在开发过程中,程序终止总是发生,并且常常导致不写出用户默认值.

The usual problem people have with user defaults in the simulator is that they kill the app (hitting stop, restarting the app, crashing) before the write takes place. In production this generally isn't an issue. If you crash right around the time you're writing to user defaults, it's pretty likely you were in a poorly defined state anyway, so whether you wrote garbage, or wrote something good, or didn't write anything is all kind of a toss-up of what you would prefer. But when developing, program termination happens all the time, and often this leads to not writing out the user defaults.

现在,一种经常工作的解决方案是在整个地方撒满 synchronize .但是在大多数情况下,这只会降低您的代码速度.如果行得通,那就好.但是,在我从事的大多数项目中,真正的解决方案是在终止应用程序之前先喘口气.不需要很多时间.只是不要在应用程序有机会写出您要保存的内容之前杀死它,然后再重新启动以检查它是否已保存.

Now one often-seems-to-work solution is to sprinkle synchronize all over the place. But for the most part that just slows your code down. If it works, fine. But in the majority of projects I've worked on, the real solution was just to take a breath before killing the app. It doesn't need a lot of time; just don't kill the app before it has a chance to write the thing you were saving before relaunching to check if it saved.

这篇关于Xcode 9模拟器不会保存用户默认设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆