John
John

Reputation: 15

Unable to save integer with NSUserDefaults

Basically I am trying to make it so when I restart the app the score for the user is saved, currently, the score reverts to zero upon closing and reopening the app. I have looked for numerous solutions on the internet but have found nothing. Here is my code (ViewController.m):

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

int bytes;
int highScore;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    highScore = bytes;

    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
    [[NSUserDefaults standardUserDefaults] synchronize];


    // Snippet used to get your highscore from the prefs.
    highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)addBytes:(id)sender {
    highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];
    highScore++;
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
    [_byteCounter setText:[NSString stringWithFormat:@"%d", highScore]];

}

@end

Upvotes: 0

Views: 105

Answers (3)

Hamed Kharazmi
Hamed Kharazmi

Reputation: 449

you can use this code, i hope work it for you:

[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:@"HighScore"];

Upvotes: 2

John
John

Reputation: 15

Upon start up I was not displaying the saved value from NSUserDefaults to the label, here is my updated code that does save the integer upon restarting the app. (I'm very proud of my self!)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

int bytes;
int highScore;

- (void)viewDidLoad {
    [super viewDidLoad];

    highScore = bytes;

    highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];
    [_byteCounter setText:[NSString stringWithFormat:@"%d", highScore]];

    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (IBAction)addBytes:(id)sender {
    highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HighScore"] intValue ];
    highScore++;
    [_byteCounter setText:[NSString stringWithFormat:@"%d", highScore]];
    [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];
}

@end

Upvotes: 0

Essam Ewaisha
Essam Ewaisha

Reputation: 439

The user defaults is not being updated as you don't update it.

highscore++ only increments the your int variable, it doesn't increment the value in NSUserDefaults. So after you update highscore, you still need to update the NSUserDefaults by calling:

[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:highScore] forKey:@"HighScore"];

Furthermore, you need not, and should not, call synchronize. The data will be flushed to the disk at some point later on automatically.

Upvotes: 0

Related Questions