我是否需要进行registerDefaults:每次应用程序启动时?

时间:2023-01-22 08:42:17

I'm developing an iOS app with latest SDK.

我正在开发一个带有最新SDK的iOS应用程序。

I do this:

我这样做:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    [standardUserDefaults registerDefaults: [PreferenceDefaultValues dictionary]];
    [standardUserDefaults synchronize];

    return YES;
}

Is it needed to do registerDefaults: every time the app starts?

是否需要进行registerDefaults:每次应用程序启动时?

2 个解决方案

#1


2  

Yes, the documentation for registerDefaults: states explicitly:

是的,registerDefaults的文档:明确说明:

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

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

Note that for the same reason there is no need to call synchronize after registerDefaults:.

请注意,出于同样的原因,在registerDefaults之后不需要调用synchronize:。

#2


1  

Yes according to the apple documentation it is necessary:

是的,根据苹果文档是必要的:

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. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.

如果没有注册域,则使用指定的字典创建一个注册域,并将NSRegistrationDomain添加到搜索列表的末尾。注册域的内容不会写入磁盘;每次应用程序启动时都需要调用此方法。您可以将plist文件放在应用程序的Resources目录中,并使用从该文件读入的内容调用registerDefaults:。

When you are registering defaults you usually provide a custom plist file. So when you change a value its written to this plist file and when you get a value it is taken from this plist file.

注册默认值时,通常会提供自定义plist文件。因此,当您更改其写入此plist文件的值时,当您获得一个值时,它将从此plist文件中获取。

#1


2  

Yes, the documentation for registerDefaults: states explicitly:

是的,registerDefaults的文档:明确说明:

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

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

Note that for the same reason there is no need to call synchronize after registerDefaults:.

请注意,出于同样的原因,在registerDefaults之后不需要调用synchronize:。

#2


1  

Yes according to the apple documentation it is necessary:

是的,根据苹果文档是必要的:

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. You can place a plist file in the application's Resources directory and call registerDefaults: with the contents that you read in from that file.

如果没有注册域,则使用指定的字典创建一个注册域,并将NSRegistrationDomain添加到搜索列表的末尾。注册域的内容不会写入磁盘;每次应用程序启动时都需要调用此方法。您可以将plist文件放在应用程序的Resources目录中,并使用从该文件读入的内容调用registerDefaults:。

When you are registering defaults you usually provide a custom plist file. So when you change a value its written to this plist file and when you get a value it is taken from this plist file.

注册默认值时,通常会提供自定义plist文件。因此,当您更改其写入此plist文件的值时,当您获得一个值时,它将从此plist文件中获取。