tags:

views:

180

answers:

3

Does anyone know how I would go about saving my integer "HighScore" to nsuserdefaults so I can load and save the integer to it . Thanks.

A: 

Have you looked at the documentation?

Mark Bessey
+2  A: 
[[NSUserDefaults standardUserDefaults] setInteger:HighScore forKey:@"HighScore"];

… to get it back:

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

This is explained very simply in the documentation.

iKenndac
A: 

More generally, you can save Foundation class objects to the user defaults, e.g.:

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

and

NSInteger highScore = [[[NSUserDefaults standardUserDefaults] objectForKey:@"kHighScore"] intValue];

But the convenience method is there for taking in an NSInteger directly.

I prefer to use the more agnostic -setObject: and -objectForKey: because it more cleanly separates the object type from access to the dictionary.

Alex Reynolds