Note that there are some explanatory texts on larger screens.

plurals
  1. PONSUserDefaults: Saved Number Always 0, iPhone
    text
    copied!<p>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:</p> <p>To save on exiting:</p> <pre><code>- (void)applicationWillTerminate: (UIApplication *) application { double save = [label.text doubleValue]; // This could be the issue //double save = 3.5; // This works, it saves the value and loads it fine, so that is not the problem here. [[NSUserDefaults standardUserDefaults] setDouble: save forKey: @"savedNumber"]; [[NSUserDefaults standardUserDefaults] synchronize]; } </code></pre> <p>To check:</p> <pre><code>- (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]; } } </code></pre> <p>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. Why would:</p> <pre><code>double save = [label.text doubleValue]; </code></pre> <p>always equal zero? Before I close the app the number in that label is roughly 0.5% (it varies). If it makes any difference I am testing this on the iPhone simulator.</p> <p>Many thanks,</p> <p>Stu</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload