NSUserDefaults won't save permanantly

2019-08-24 15:43发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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