NSUserDefaults: Saved Number Always 0, iPhone

Posted by Stumf on Stack Overflow See other posts from Stack Overflow or by Stumf
Published on 2010-04-18T20:21:17Z Indexed on 2010/04/18 20:23 UTC
Read the original article Hit count: 263

Filed under:
|
|
|
|

Hello all,

I have looked at other answers and the docs. Maybe I am missing something, or maybe I have another issue. I am trying to save a number on exiting the app, and then when the app is loaded I want to check if this value exists and take action accordingly. This is what I have tried:

To save on exiting:

- (void)applicationWillTerminate: (UIApplication *) application
{
 double save = [label.text doubleValue];

 [[NSUserDefaults standardUserDefaults] setDouble: save forKey: @"savedNumber"];
 [[NSUserDefaults standardUserDefaults] synchronize]; 
}

To check:

- (IBAction)buttonclickSkip{

 double save = [[NSUserDefaults standardUserDefaults] doubleForKey: @"savedNumber"];

 if (save == 0) {

    [self performSelector:@selector(displayAlert) withObject:nil];

    test.enabled = YES;
    test.alpha = 1.0;

    skip.enabled = NO;
    skip.alpha = 0.0;

   }

 else {

 label.text = [NSString stringWithFormat:@"%.1f %%", save]; 
}

}

The problem is I always get my alert message displayed, the saved value is not put into the label so somehow == 0 is always true. If it makes any difference I am testing this on the iPhone simulator.

Many thanks,

Stu

© Stack Overflow or respective owner

Related posts about nsuserdefaults

Related posts about iphone