NSUserDefaults没有加载我想要的数字

时间:2014-03-29 08:22:50

标签: xcode nsuserdefaults

我正在尝试使用NSUserDefaults在app关闭后保存两个变量。一个是firstBoot而另一个是showAgain。准备好代码 -

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults]; //Here is where it is supposed to be loading the firstBoot variable to check whether or not it is the first boot
    NSInteger boot = [defaults1 integerForKey:@"firstBoot"];
    firstBoot = boot; //Sort of pointless variable swapping

    if (firstBoot == 0) //Detects if isn't first boot, checks whether or not showAgain is yes or no (1=yes, 0=no)
    {
        NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
        NSInteger boot = [defaults1 integerForKey:@"firstBoot"];
        NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
        NSInteger show = [defaults2 integerForKey:@"showAgain"];
        firstBoot = boot; //More var swapping
        showAgain = show; //^^
    }
    else
    {
        firstBoot = 1; //If it is the first boot, or the variable wasn't 0, it sets them both to 1.
        showAgain = 1;
    }
    if ((firstBoot == 1) || (showAgain == 1)) //Checks if its the firstBoot or showAgain is set to yes
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Check the 'About' tab for help!" //Displays alert
                                                message:@""
                                               delegate:self
                                      cancelButtonTitle:@"Don't show again"
                                      otherButtonTitles:@"OK", nil];
        [alert show];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"firstboot and showagain != 1" //Displays alert if it isn't the first boot, or isn't 1 and showAgain isn't set to yes(1).
                                                    message:@""
                                                   delegate:self
                                          cancelButtonTitle:@"wrong"
                                          otherButtonTitles:@"OK", nil];
        [alert show];
    }
}

快讯 -

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == [alertView cancelButtonIndex]) 
    {
        showAgain = 0; //If user clicks "Don't show again", set showAgain to off(0) and tell it it isn't the first boot anymore
        firstBoot = 0;

        NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
        [defaults1 setInteger:0 forKey:@"firstBoot"]; //Hopefully stores firstBoot as 0
        [defaults1 synchronize];

        NSUserDefaults *defaults2 = [NSUserDefaults standardUserDefaults];
        [defaults2 setInteger:0 forKey:@"showAgain"]; //Again storing showAgain as 0
        [defaults2 synchronize];
    }
}

第一次警报应该在您第一次运行应用程序时弹出,而第二次警报应该是调试器占位符,并且应该在每次第一次运行后运行。对我来说,第二个是先运行,告诉我firstBoot和showAgain!= 1。

2 个答案:

答案 0 :(得分:0)

第一次启动时firstBoot如何设置为YES?谁会设置它?

调用registerDefaults:在应用程序开始时,在明确设置之前,应该具有不等于0的值的所有属性。例如,使用键firstBoot = YES。或者,使用属性“notFirstBoot”,其中缺少属性的返回值(“NO”)是您实际需要的。

实际上没有必要一次又一次地调用standardUserDefaults。

答案 1 :(得分:0)

NSInteger boot = [defaults1 integerForKey:@"firstBoot"];将返回0,直到您在默认值中保存了值。对于未存储任何值的密钥,将返回0NOnil

处理此问题的正确方法是在尝试检索这些用户默认值之前注册默认值。

// register default values. until you save your own value "firstBoot" will return YES
// you should put these two lines in the `application:didFinishLaunchingWithOptions:`
// method of the AppDelegate. Add all userdefaults that should have default values there
NSDictionary *defaultUserDefaults = @{ @"firstBoot" : @YES };
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultUserDefaults];

// use BOOL instead of integer
BOOL firstBoot = [[NSUserDefaults standardUserDefaults] boolForKey:@"firstBoot"];
if (firstBoot) {
    NSLog(@"first launch");
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstBoot"];
    [[NSUserDefaults standardUserDefaults] synchronize]; 
}
else {
    NSLog(@"has been launched before");
}