-1

I am trying to save an NSInteger (label) into NSUserDefaults but I have not found a way to do this is it even possibel

Saving the data

[[NSUserDefaults standardUserDefaults] setInteger:label forKey:@"key1"];

Retrieving the data

- (void)viewDidLoad {
label = [[NSUserDefaults standardUserDefaults] integerForKey:@"key1"];
[super viewDidLoad];
nsinvocation
  • 7,559
  • 3
  • 41
  • 46
  • 1
    You need to add some more information here about the problems you're having. What doesn't work here? Try adding some NSLog statements demonstrating the problem and show the output. – Jesse Rusak Jul 29 '12 at 13:52
  • 1
    That code is correct. What's your question? – Dave DeLong Jul 29 '12 at 13:52
  • Given that the above code works, and you stated that it was an issue with adding the numbers, I'm closing this. Please feel free to ask a new question with the addition issues that you are running into, if that's your core problem. – Brad Larson Jul 30 '12 at 14:37

2 Answers2

6

I think you forgot so synchronize your settings. Try this:

[[NSUserDefaults standardUserDefaults] setInteger:label forKey:@"key1"];
[[NSUserDefaults standardUserDefaults] synchronize];
Andrew
  • 24,218
  • 13
  • 61
  • 90
  • ok i guess then my problem isn't the saving but the adding numbers. if i have an int called label and make it so that label + 1 is that workabel code? – Matt Mühlemann Jul 29 '12 at 14:19
  • @MattMühlemann: I don't understand your question. I think it's better if you ask a new one as John suggested – Andrew Jul 29 '12 at 14:55
4

What you are doing works perfect

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

NSInteger highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScore"];

duplicate: How do you save an integer to NSUserDefaults?

Read the documentation on this topic, it looks like you don't understand NSUserDefaults

Community
  • 1
  • 1
John Riselvato
  • 12,854
  • 5
  • 62
  • 89