bundle返回错误的值

bundle返回错误的值

本文介绍了iPhone-阅读Setting.bundle返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Settings.bundle,它带有一个Root.plist文件和一个本地化目录en.lproj.

I have created a Settings.bundle that came with a Root.plist file and a localization directory en.lproj.

我已经编辑了Root.plist并添加了一些我想要为我的应用设置的设置.

I have edited Root.plist and added several settings I want to have for my app.

当我从iPhone删除该应用并安装并首次运行时,我读取的所有设置都返回错误的值.例如:

When I delete the app from iPhone and install it and run the first time, all settings I read return wrong values. For example:

highQualityFlag = [[[NSUserDefaults standardUserDefaults] stringForKey:@"qualityFlag"] boolValue];

即使设置默认值为是",标记也为否".

the flag comes as NO, even if the setting default is YES.

如果我更改了某些设置并再次运行,则所有后续运行都会为我提供正确的值(??)

If I change something on the settings and run again, all subsequent runs give me the correct values (??)

我该如何解决?

谢谢

推荐答案

尝试一下:

- (void)registerDefaultsFromSettingsBundle
{
    NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
    if(!settingsBundle)
    {
        //NSLog(@"Could not find Settings.bundle");
        return;
    }

    NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
    NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];

    NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
    for(NSDictionary *prefSpecification in preferences)
    {
        NSString *key = [prefSpecification objectForKey:@"Key"];
        if(key)
        {
            [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
    [defaultsToRegister release];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self registerDefaultsFromSettingsBundle];

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        [window makeKeyAndVisible];
    return YES;
}

这篇关于iPhone-阅读Setting.bundle返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 00:11