NSUserDefaults如何注册默认工作? [英] How does NSUserDefaults registerDefaults work?

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

问题描述

当我在应用程序中设置registerDefaults时:didFinishLaunchingWithOptions:我通过应用程序设置NSUserDefaults的默认值。

When i set registerDefaults in application:didFinishLaunchingWithOptions: i set the default values for the NSUserDefaults throught the app.

NSMutableDictionary *defaultsDictionary = [@{@"userHasLoggedInOnce":@NO, @"firstTimeOpeningApp":@YES} mutableCopy];

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsDict];
[[NSUserDefaults standardUserDefaults] synchronize];

registerDefaults如何存储它只设置了这些值一次,因为每次在应用启动时调用它?它是一个自己的值,也设置为standardUserDefaults值?如果是这样,是否可以重置默认值?

How does registerDefaults store that it has set these values only once, since its called every time on app start? Is it an own value thats also set as a standardUserDefaults value? If so, is it possible to reset the default values?

推荐答案

注册用户默认值为键设置默认值。因此,当应用程序第一次启动时,您将不会得到nil,0或false,然后必须通过代码测试很多次(如果是这种情况)。而是设置类似欢迎的东西。

Register user defaults sets default values, for the keys. So when the application starts for the first time, you won't get nil, 0 or false and then have to test a lot of times through your code if that's the case. Instead you set something like "Welcome".

编辑:
注册域的内容不会写入磁盘;每次应用程序启动时都需要调用此方法。您可以将plist文件放在应用程序的Resources目录中,并使用从该文件读入的内容调用registerDefaults:。 (来源

正如评论所解释的那样,调用registerUserDefaults并不会覆盖savedValues,因此您每次都可以安全地调用它应用程序启动。您可以添加一个重置按钮,用户可以使用plist中的值覆盖保存的值(默认值)。

As the comment explained, calling registerUserDefaults doesn't override the savedValues, so you can safely call it each time the application launches. You can add a reset button, where you override the saved values by the user, with the values in your plist (defaults).

这篇关于NSUserDefaults如何注册默认工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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