views:

185

answers:

3

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

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. Why would:

double save = [label.text doubleValue];

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.

Many thanks,

Stu

A: 

My guess would be that the text in your label is not a valid double value. From the Apple docs for NSString -doubleValue:

Returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.

Make sure you are passing it something like @"13.2". The best way to check this is to stick a NSLog call right after you create the variable save.

pheelicks
Hmm this is my line putting a value into the label in the first place:label.text = [NSString stringWithFormat:@"%.1f %%", number];The actual label therefore contains numbers like "12.7%"
Stumf
I changed the label to 3.5 with no percent symbol, still shows alert.
Stumf
+1  A: 

Make sure that your applicationWillTerminate: implementation is in your app delegate class.

jlehr
I added some new info to the question. applicationWillTerminate being hit now.
Stumf
+1  A: 

The fact that you can hard-code the value and fetch it back means the problem definitely revolves around your interaction with the label.text and not your use of NSUserDefaults.

Make sure that the label has not already been destroyed at the time you go to fetch its value. As the application is terminating it may have already brought down the view from which you are fetching the value.

Another thing to try would be to get the actual text itself instead of asking the OS to convert the text value into a number first. If you print that out you may get some clue as to what is going on.

fbrereto
Your theory that the label had already been destroyed seems to have been correct. Working now. Thanks to all who helped!
Stumf