NSUserDefaults不会永久保存 [英] NSUserDefaults won't save permanantly

查看:48
本文介绍了NSUserDefaults不会永久保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的iPhone应用程序.但是,我无法让我的NSUserDefaults永久保存.写入和检索数据不是问题-即使在切换视图,关闭/打开应用程序等之后,我也可以保存数据(以我的情况为字符串),并通过命令检索它,而只是检索数据美好的.看起来好像字符串已正确保存到键中.但是,当我从多任务托盘中退出应用程序并重新启动它时,设置将不再保存,并且应用程序像第一次启动一样启动.我是新手程序员,所以对我来说,这可能只是一个愚蠢的错误.

I have a fairly simple iPhone application that I am making. However, I cannot get my NSUserDefaults to save permanently. Writing and retrieving the data is not a problem––I can save the data (in my case, a string), and retrieve it on command, even after switching views, closing/opening the application, etc, and the data is retrieved just fine. It appears as if the string was saved properly to the key. But when I quit the application from the multitasking tray and restart it, the settings are no longer saved, and the application boots up like it is the first time. I am a novice programmer, so it is probably just a stupid error on my part.

这是节省的样子:

if (optionsSoundBox.center.x >= 240)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.2];
        self.soundOnLabel.alpha = 1;
        self.soundOffLabel.alpha = 0;
        [UIView commitAnimations];

        NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
        [soundOptions setObject:@"SoundsON" forKey:@"SoundKey"];
        [soundOptions synchronize];
    }

    if (optionsSoundBox.center.x < 240)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:.2];
        self.soundOnLabel.alpha = 0;
        self.soundOffLabel.alpha = 1;
        [UIView commitAnimations];

        NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
        [soundOptions setObject:@"SoundsOFF" forKey:@"SoundKey"];
        [soundOptions synchronize];
    }

我在viewDidLoad中检索了字符串,因此它将在启动时像这样准备好:

I retrieve the string in the viewDidLoad so it will be ready on startup like this:

NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
    NSString *savedSoundSettings = [soundOptions stringForKey:@"SoundKey"];

    if (savedSoundSettings == @"SoundsON")
    {
        [optionsSoundBox setCenter:CGPointMake(280, optionsSoundBox.center.y)];
    }

    if (savedSoundSettings == @"SoundsOFF")
    {
        [optionsSoundBox setCenter:CGPointMake(200, optionsSoundBox.center.y)];
    }

我非常感谢您能提供的任何帮助

I really appreciate any help you can give

推荐答案

您应该使用 [savedSoundSettings isEqualToString:@"SoundsON"] ,而不要使用double equals运算符.( == ).

You should use [savedSoundSettings isEqualToString:@"SoundsON"], instead of using the double equals operator. (==).

除此之外,这可能是您在 viewDidLoad 中运行此代码的事实.尝试改为在 viewWillAppear 内部运行它.

Aside from that, it could be the fact that you are running this code inside of viewDidLoad. Try to run it inside of viewWillAppear instead.

最后,我建议使用驼峰名称作为设置键,从长远来看,键入起来会更容易.(请考虑使用 soundsOn 而不是 SoundsON .)

Finally, I recommend using camelcase names for settings keys, so it will be easier to type in the long run. (Think soundsOn instead of SoundsON.)

这篇关于NSUserDefaults不会永久保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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