ios 如何为 NSUserDefault Keys 设置初始值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8473986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:50:33  来源:igfitidea点击:

How to set initial values for NSUserDefault Keys?

iosplistnsuserdefaultsuser-experience

提问by Suz

I want to set some initial values for my NSUserDefault keys so that the first run of the app has some reasonable initial settings. I thought I ran across a simple way to do this in the app bundle .plist, but now I can't find it. Any ideas?

我想为我的 NSUserDefault 键设置一些初始值,以便应用程序的第一次运行具有一些合理的初始设置。我以为我在应用程序包 .plist 中遇到了一个简单的方法来做到这一点,但现在我找不到它。有任何想法吗?

回答by Ole Begemann

You should use the registerDefaultsmethod of NSUserDefaults. Prepare a plist file in your bundle that contains the default preferences and then use that plist to register the defaults.

您应该使用 的registerDefaults方法NSUserDefaults。在包中准备一个包含默认首选项的 plist 文件,然后使用该 plist 注册默认值。

NSString *defaultPrefsFile = [[NSBundle mainBundle] pathForResource:@"defaultPrefs" ofType:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfFile:defaultPrefsFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultPreferences];

You have to execute this code on every launch of your app. It will add these values to a separate domain in the user defaults hierarchy. Whenever your app's user defaults don't provide a value for a certain key, NSUserDefaultswill fall back to this domain and retrieve the value from there.

您必须在每次启动应用程序时执行此代码。它将这些值添加到用户默认层次结构中的单独域中。每当您的应用程序的用户默认值没有为某个键提供值时,NSUserDefaults就会回退到该域并从那里检索该值。

回答by thanhbinh84

If you have many default values, let use ola's answer, otherwise this is good for a few params

如果您有许多默认值,请使用ola 的答案,否则这对一些参数有好处

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if (![defaults boolForKey:USERDEFAULT_IS_INITIALIZED]) { 
    [defaults setBool:YES forKey:USERDEFAULT_IS_INITIALIZED];

    // Set initial values 
    ...

    [defaults synchronize];
}

回答by James Webster

if ([[[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys] containsObject:@"initialValuesHaveBeenWritten"])
{
    [[NSUserDefaults standardUserDefaults] setValue:obj1 forKey:key1];
    [[NSUserDefaults standardUserDefaults] setValue:obj2 forKey:key2];


    [[NSUserDefaults standardUserDefaults] setValue:obj1 forKey:@"initialValuesHaveBeenWritten"];

    [[NSUserDefaults standardUserDefaults] synchronize];
}

NB: Not tested, done from memory

注意:未经测试,凭记忆完成

回答by Gank

-(void) loadDef
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    _removeAd=[userDefaults boolForKey:SAVE_AD_STATUS];

    NSString* strDefSetting=[userDefaults stringForKey:SAVE_STATUS_ADSETTING];
    if(strDefSetting==nil
       ||[strDefSetting isEqualToString:@""]
       )
    {
        strDefSetting=@"0.5";
    }

    _floatAdmob=strDefSetting.floatValue;//0.5;
}