了解 UserDefaults 注册方法 [英] Understanding the UserDefaults register method

查看:50
本文介绍了了解 UserDefaults 注册方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我说这不是抱怨,我只是想了解 register(defaults: []) 方法是如何工作的.

First of all, let me say that this is NOT a complaint, I just want to understand how does the register(defaults: []) method works.

我在 didFinishLaunchingWithOptions 方法中使用 UserDefaults register(defaults: []) 方法来注册我所有 UseDefault 键的默认值,一切正常没问题.

I'm using the UserDefaults register(defaults: []) method inside the didFinishLaunchingWithOptions method to register the default value of all of my UseDefault keys, everything is working fine as expected.

我的问题是,为什么每次调用 didFinishLaunchingWithOptions 方法时 register(defaults: []) 方法中的值不会重置?

My question is, why is that the values in the register(defaults: []) method do not reset everytime the didFinishLaunchingWithOptions method is called?

我不想让他们休息,我只是想了解原因.

I do not want them to rest I'm just trying to understand why.

我有以下代码...

func application(...didFinishLaunchingWithOptions...) -> Bool {

    UserDefaults.standard.register(defaults: [
        keyUserName:"",
        keyHasCar:false
        ])
}

这就是为什么我的困惑,当应用程序运行时,它会按预期保存默认值,然后,如果这些值稍后在应用程序的某个地方被修改,并且在应用程序被终止并重新启动后,didFinishLaunchingWithOptions 再次被调用,但值不会被重置,但如果我向数组中添加一个新键,它会被保存,但其余的键不会被重置,只有新的键被添加为默认值.

Here is why my confusion, when the app runs, it saves the default values as expected, then, if the values get modified later somewhere in the app and right-after the app is killed and relaunched, the didFinishLaunchingWithOptions is called again but the values do not get reset, but if I add a new key to the array it does get saved but the rest of the keys do not get reset only the new one is added with its default value.

同样,我不想重置这些值,我只需要了解 register 方法是如何工作的.

Again, I do not want the values to reset, I just need to understand how does register method works.

推荐答案

来自 register(defaults:) 的文档:

如果没有注册域,则使用指定的字典创建一个,并将 NSRegistrationDomain 添加到搜索列表的末尾.

If there is no registration domain, one is created using the specified dictionary, and NSRegistrationDomain is added to the end of the search list.

注册域的内容不写入磁盘;每次应用程序启动时都需要调用此方法.

The contents of the registration domain are not written to disk; you need to call this method each time your application starts.

这意味着注册的默认值充当您正在使用的正常用户默认值的后备.注册的默认值不会覆盖或替换标准用户默认值.

What this means is that the registered defaults act as a fallback to the normal, user defaults that you are working with. The registered defaults do not overwrite or replace the standard user defaults.

当您尝试从 UserDefaults.standard 查找密钥时,如果该密钥不存在,则会在已注册的默认值中查找并返回该结果(如果有).

When you attempt to lookup a key from UserDefaults.standard, if the key isn't there, then it is looked up in the registered defaults and that result (if any) is returned.

一旦您在 UserDefaults.standard 中设置了一个值,当然它就是为键返回的值.如果您从 UserDefaults.standard 中删除密钥,则会再次使用注册的默认值.

Once you set a value in UserDefaults.standard, of course it is that value that is returned for the key. If you remove the key from UserDefaults.standard then the registered default is used again.

这篇关于了解 UserDefaults 注册方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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