如何从Objective-c/Swift中的settings.bundle中检索值? [英] How to retrieve values from settings.bundle in Objective-c/Swift?

查看:101
本文介绍了如何从Objective-c/Swift中的settings.bundle中检索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个项目,用于设置和检索 settings.bundle 中的值.我还在 settings.bundle 文件中设置了一些默认值.现在的问题是当我将值检索为

I have created a project that set and retrieve values from settings.bundle. I have also set some defaults values in settings.bundle file. Now the problem is when I retrieve values as

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
loginName.text = [defaults objectForKey:@"login_name"];

第一次显示为空,但这些值在iPhone应用程序设置中设置. 如果我更改值或手动设置,则可以正确检索值.

for the first time it shows null, but the values get set in iPhone application settings. If I change the values or set it manually, then values are retrieved properly.

帮帮我

推荐答案

尽管您定义了默认设置,但它们并未真正存储为值.它们被默认存储.如果您尝试读取它,则该值为null.默认设置是值的另一个属性.但这并不意味着会将默认值写为默认值.

Although you define the defaults settings, they are not really stored as a value. They are stored as default. If you try to read it, the value is null. Default setting is another property as value is. But it doesnt mean that will write the default value as a default.

我要做的是,首先检查某些设置(我确定应该有一个值)是否存储了任何内容.如果没有任何内容,那么我会写所有默认值.

What I do is, first, check if some setting,(that I'm sure that should have a value) has anything stored on it. If it doesn't have anything then I write all the defaults.

这里是一个例子.

在AppDelegate.m上,我检查 email_notificaciones_preference 是否具有值,如果没有,我将所有默认设置写入每个设置.

on AppDelegate.m I check if email_notificaciones_preference has a value, if not, I write ALL default settings to each setting.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults];
    NSString * email_notificaciones_preference = [standardUserDefaults objectForKey:@"email_notificaciones_preference"];
    if (!email_notificaciones_preference) {
        [self registerDefaultsFromSettingsBundle];
    }

}

此功能是我用来将默认值写入每个元素的功能.

This function is what I use to write defaults to each element.

#pragma NSUserDefaults
- (void)registerDefaultsFromSettingsBundle {
    // this function writes default settings as settings
    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];
            NSLog(@"writing as default %@ to the key %@",[prefSpecification objectForKey:@"DefaultValue"],key);
        }
    }

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];

}

希望有帮助.

这篇关于如何从Objective-c/Swift中的settings.bundle中检索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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