CherryBrandy
CherryBrandy

Reputation: 55

Help needed on saving and loading data using NSUserDefaults

I am working on a new iPhone app and at the moment I am trying to save and load highscores using NSUserDefaults. My question is : what code exactly should I use to assign the highscores I have saved to a variable called highscorespointer1, which is a pointer to an NSInteger? I am currently using the code shown below:

Here i am saving the highscore. Note that the variable highscore1 is of type NSInteger:

-(IBAction)saveData{
      NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setInteger:highscore1 forKey:@"H1"];
    [defaults synchronize];
           }

And here i am loading the highscore.But i get this warning: "Assignment from incompatible pointer type":

highscorepointer1 =  [[NSUserDefaults standardUserDefaults] objectForKey:@"H1"];

Upvotes: 2

Views: 428

Answers (1)

msgambel
msgambel

Reputation: 7340

The setInteger:forKey method sets an int, not an int *, meaning it sets a value, and not a pointer. Make sure your highscorepointer1 is an int, not an int *. It doesn't make sense to save the pointer to the integer anyways, but the actual integer itself should be saved. Hope that Helps!

Upvotes: 1

Related Questions