NSUserDefaults 不会永久保存

发布于 2024-11-15 10:51:23 字数 1662 浏览 2 评论 0原文

我正在制作一个相当简单的 iPhone 应用程序。但是,我无法永久保存我的 NSUserDefaults 。写入和检索数据不是问题——我可以保存数据(在我的例子中是一个字符串),并根据命令检索它,即使在切换视图、关闭/打开应用程序等之后,也可以检索数据美好的。看起来好像字符串已正确保存到密钥中。但是,当我从多任务托盘中退出应用程序并重新启动它时,设置不再保存,并且应用程序像第一次一样启动。我是一名新手程序员,所以这可能只是我的一个愚蠢的错误。

保存的样子如下:

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 中检索字符串,以便它在启动时准备就绪,如下所示:

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 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.

Here's what the saving looks like:

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];
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

埋葬我深情 2024-11-22 10:51:23

而且因为您得到的是 stringForKey: 而不是 objectForKey:

如果您只使用 [[NSUserDefaults standardUserDefaults] setBool: YES forKey: 也许对您来说会更容易@"SoundsON"]; 然后检查 boolForKey: @"SoundsON" 是否为 true。

And also because you're getting stringForKey: instead objectForKey:

Maybe it would be easier for you if you just used [[NSUserDefaults standardUserDefaults] setBool: YES forKey: @"SoundsON"]; and then check if boolForKey: @"SoundsON" is true.

天涯离梦残月幽梦 2024-11-22 10:51:23

您应该使用[savedSoundSettings isEqualToString:@"SoundsON"],而不是使用双等于运算符。 (<代码>==)。

除此之外,可能是您在 viewDidLoad 内部运行此代码。尝试在 viewWillAppear 内部运行它。

最后,我建议使用驼峰命名法作为设置键,这样从长远来看打字会更容易。 (想想 soundsOn 而不是 SoundsON。)

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

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.

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.)

昇り龍 2024-11-22 10:51:23

您不应将字符串与 == 进行比较。比较字符串的正确方法是通过 isEqualToString:。因此,您在检索用户默认值时的 if 语句应如下所示:

if ([savedSoundSettings isEqualToString:@"SoundsON"])
{
    ....
}

编辑:此外,从概念上讲,您要检查的只是单个状态变量是 ON 还是 OFF。因此,理想情况下,您应该使用 [NSUserDefaults setBool:forKey:][NSUserDefaults boolForKey:] 之类的东西。

You should not compare strings with ==. The correct way to compare strings is via isEqualToString:. So your if statements while retrieving the user defaults should look like:

if ([savedSoundSettings isEqualToString:@"SoundsON"])
{
    ....
}

EDIT: Moreover, conceptually, all you are checking is whether a single state variable is ON or OFF. So you should ideally be using something like [NSUserDefaults setBool:forKey:] and [NSUserDefaults boolForKey:].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文