简体   繁体   English

Swift UserDefaults先前的数据输入

[英]Swift UserDefaults prior data entry

Is there any way to populate UserDefaults with data before running the app so something can be added to it during runtime. 有什么方法可以在运行应用程序之前用数据填充UserDefaults,以便可以在运行时向其中添加一些内容。 For example say I want "Apples", "Bananas" and "Carrots" to be added manually prior to the application running in UserDefaults for use whenever, but then want to add "Dill" and "Eggplant" so the next time the application runs all A, B, C, D and E are available in the UserDefaults 例如,说我希望在应用程序在UserDefaults中运行之前手动添加“苹果”,“香蕉”和“胡萝卜”,以便随时使用,然后再添加“莳萝”和“茄子”,以便下次应用程序运行时所有的A,B,C,D和E在UserDefaults中都可用

struct MyVariables
{
    let ArrayList = UserDefaults.standard.object(forKey: "ArrayList")
}

UserDefaults has a class method register(defaults:) that you can use to register a dictionary of "default defaults". UserDefaults具有一个类方法register(defaults:) ,可用于注册“默认默认值”的字典。 Take a look at the docs for more details. 查看文档以获取更多详细信息。

You can create singleton DataManager and create custom init method. 您可以创建单例DataManager并创建自定义init方法。 In this init you can specify, how the table should be populated, when the data should be added/removed. 在此init中,您可以指定应如何填充表,何时应添加/删除数据。

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

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