I've been searching around for tutorials on how to save certain things to NSuserdefaults but have only found stuff mostly dealing with arrays and strings. Can anyone lead me to or give some person knowledge about saving and loading a int. For my application I have my int highScore = 0; but I want it to save this int into your NSuserdefaults so when my game loads back up it displays your current high score you recently achieved.
+2
A:
Save it as a NSNumber
, using -[NSNumber numberWithInt:highScore]
.
Steven Canfield
2009-07-12 01:32:24
Thanks. Will try it out.
2009-07-12 02:13:52
+6
A:
int highScore = 0;
// write
[[NSUserDefaults standardUserDefaults] setInteger:highScore forKey:@"someKey"];
// read
highScore = [[NSUserDefaults standardUserDefaults] integerForKey:@"someKey"];
Kevin L.
2009-07-12 01:34:35
where should I put the write and read at in my code. Should one be called when my superview loads.. and the other when submitting the highscore ? I tried it and didn't get in errors but it wouldn't save the data.
2009-07-12 03:21:33
Write when submitting the high score and read when loading view that shows the high score to the user (maybe in -viewDidLoad or -viewWillAppear).
Kevin L.
2009-07-12 04:04:38
+1
A:
I've written a short tutorial that might be of help, as it shows how to work with a boolean and an integer
John
2009-07-12 15:39:59