views:

1035

answers:

2

(Problem solved now, see answers below)

Hello, in my app I am trying to create a points counter. That increases a number by 10 every time a button is pressed.

I successfully managed to get my app to increase a value (0) by 1 every time a button is pressed, and to remember the latest value and display it next time the app starts. But when you press the button that's suppose to increase the points value by 1 after the app restarts the number starts increasing from 0 not the remembered value.

How would I get the points to increase by 10 and remember the latest value and display (and count from it) after the application is exited and started again ?

Here is my current code:

Code for the button to increase the value by 1:

   - (IBAction)startClick:(id)sender{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:count forKey:@"greeting"];

NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count++];
counter.text = numValue;

viewDidLoad method:

   - (void)viewDidLoad {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *greetName = [prefs stringForKey:@"greeting"];
NSInteger count = [prefs integerForKey:@"greeting"];
counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
 counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
 counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}
+1  A: 

In your startClick: method, you appear to store the count integer in the user defaults before it has been incremented. If you want to increment it by 10, then change the method to:

- (IBAction)startClick:(id)sender {
    //Increase count by 10
    count+=10;

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    [prefs setInteger:count forKey:@"greeting"];

    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", count];
    counter.text = numValue;
}

Also, why do you set the counter.text property twice?:

counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];

if(count == 0) {
        counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
} else {
        counter.text = [[NSString alloc] initWithFormat:@"%@",greetName];
}

It may also be useful for you to use stringWithFormat instead of allocating an NSString and calling initWithFormat, since at the moment it is leaking memory because you don't release it; if you call stringWithFormat, it will return an autoreleased object, which will be retained when counter.text is set (since it is a shorthand for the setter method) so you will have no memory problems. You can change the line to:

counter.text = [NSString stringWithFormat:@"%d",count];

Which will also save you from retrieving the counter value as a string from the user defaults.

Perspx
Okay thanks for that, It increases by 10 now, but the trouble is that when the app is quit and started again, it doesn't add 10 to the saved number but just continues counting from 10.
Amanda
Take a look at Louis Gerbarg's answer - I think that should answer your question.
Perspx
+1  A: 

There are a couple of things going on here. First off, you are saving count before you are incrementing it. That means even if the save was happening it would be one lower than you expect.

The actual bug that you are seeing though is that you are eliding count. If you look you will notice that you have the following line in viewDidLoad:

NSInteger count = [prefs integerForKey:@"greeting"];

despite the fact that directly refer to count in startClick:. I presume you also have an ivar named count and that what is going on is you mean to set the ivar, but you are in fact setting the local variable. The ivar will be set to 0 by default when the class is alloced, which is why it always seems to be zeroed. You can fix it by changing that like to:

count = [prefs integerForKey:@"greeting"];

It is also possible your values are not being synched out to disk if you are crashing during application quit, but unless you are seeing crashes in the run log that should not be an issue. You can explicitly synchronize out the value if it is important.

Are you compiling this code with warnings turned on? Are you trying to fix those warnings? If you have warnings turned on you should be seeing something like "local declaration of 'count' hides instance variable"

Louis Gerbarg
Thanks so much, It did the trick perfectly! Oh and nope, I don't get any warnings. And warnings are definitely turned on.
Amanda