iPhone-注册的默认首选项未写入plist文件 [英] iPhone - Registered default prefs are not written to plist file

查看:211
本文介绍了iPhone-注册的默认首选项未写入plist文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序didFinishLaunchingWithOptions上有以下代码(非常简单):

I have this (much much simplier) code on my app didFinishLaunchingWithOptions :

    NSDictionary *userDefaultsDefaults = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultsPrefs" ofType:@"plist"]];
    [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];

    NSLog(@"--------------- NSUserDefaults dump : %@", [prefs dictionaryRepresentation]);
    BOOL a = [[NSUserDefaults standardUserDefaults] synchronize];

但是什么也没写...我的意思是,没有写任何首选文件。如果我强制 [prefs setValue:someValue forKey:@ someKey]; ,则仅使用此键/值创建文件

But nothing is written... I mean, no pref file is written. If I force [prefs setValue:someValue forKey:@"someKey"];, then the file is created with only this key/value

为什么?
如何设置所有默认首选项,然后将它们写入磁盘?

Why ? How may I set all default preferences then write them to disk ?

BOOL a是YES。

NSLog返回整个DefaultPrefs文件内容,以及一些系统值,例如:

BOOL a is YES.
NSLog return the whole DefaultPrefs file content, plus some system values like :

AppleICUForce24HourTime = 1; 
AppleKeyboardsExpanded = 1;


推荐答案


我该如何设置所有默认的首选项然后将它们写入磁盘?

How may I set all default preferences then write them to disk ?

为什么?没有任何内容写入磁盘是默认设置的全部内容。它们是默认值,不是由用户设置的,可以在程序的更高版本中进行更改。

不希望将它们保存在首选项文件中。保存后,您将无法区分用户更改的首选项和默认值,因此无法用较新的默认值替换它们。

why? That nothing is written to disk is the whole point of defaults. They are default values, they are not set by the user, and they can change in a later version of your program.
It's not wanted that they are saved in the preferences file. Once they are saved you can't distinguish between preferences that have been changed by the user and default values, and therefore you can't replace them with newer defaults.

此行为完全相同。

如果要写入首选项文件,请使用 [[NSUserDefaults standardUserDefaults] setObject:foo forKey:bar]

If you want to write to the preferences file use [[NSUserDefaults standardUserDefaults] setObject:foo forKey:bar]

我想如果您愿意,可以使用for循环来完成真的想要。

I guess you can use a for loop to do it if you really want.

for (NSString *key in userDefaultsDefaults) {
    [[NSUserDefaults standardUserDefaults] setObject:[userDefaultsDefaults objectForKey:key] forKey:key];
}

但是如果您确实需要保存这些默认值,则应该重新考虑。我想不出这样做的正当理由。

But you should reconsider if you really need to save those defaults. I can't think of a valid reason to do this.

这篇关于iPhone-注册的默认首选项未写入plist文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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