简体   繁体   English

Xcode 9模拟器不会保存用户默认设置

[英]Xcode 9 simulator doesn't save userdefaults

Maybe it's the same issue as this one. 也许是同样的问题,因为一个。 But few of the solutions that I have tried don't work. 但是,我尝试过的解决方案中很少有行不通的。

The simulator don't save userdefaults on Xcode Version 9.0 beta 6 (9M214v). 模拟器不会在Xcode 9.0 beta 6(9M214v)上保存用户默认设置。 I had Xcode 8 as well (together with 9) but removed it. 我也有Xcode 8(连同9),但是将其删除了。

the code that I'm using: 我正在使用的代码:

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

Additionally I have tried: 另外我尝试了:

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

There are a lot of misunderstandings about what synchronize does and how user defaults are written. 关于synchronize功能以及如何编写用户默认值,存在许多误解。 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. 对git的类比也很有用,请参阅有关iOS 8更改的笔记。 NSUserDefaults changes way more than you would think it would for such an old piece of tech. 与如此古老的技术相比,NSUserDefaults的更改方式比您想象的要多。

The short and long of it is that calling synchronize is almost never needed in a production app. 总之,在生产应用程序中几乎不需要调用synchronize (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. 因为此方法是定期自动调用的,所以仅当您无法等待自动同步时(例如,如果您的应用程序将要退出),或者您想要将用户默认值更新为磁盘上的默认值时,才使用此方法。您尚未进行任何更改。

And even those cases mostly relate to macOS. 甚至那些案例大多与macOS有关。

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. 对于在iOS 9.3和更高版本/ macOS Sierra和更高版本上运行的应用程序,即使在这种情况下[进程之间同步],也不需要-synchronize(或建议),因为默认值的键值观察现在可以在进程之间进行,因此读取过程可以只是直接关注价值的变化。 As a result of that, applications running on those operating systems should generally never call synchronize. 结果,在那些操作系统上运行的应用程序通常不应调用同步。

BUT..... 但.....

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. 但是在开发时,程序终止总是发生,并且经常导致不写出用户默认值。

Now one often-seems-to-work solution is to sprinkle synchronize all over the place. 现在,一种经常在工作中使用的解决方案是在整个地方进行synchronize 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. 只是不要在应用程序有机会在重新启动以检查它是否保存之前写出您要保存的内容之前将其杀死。

for me I was using a library from git 对我来说,我正在使用git的库
this library was removing all userDefaults. 该库正在删除所有userDefaults。
As described in this answer 如本答案所述

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

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