我有一个名为int
的{{1}},我希望将其保存到手机的内存中,这样即使游戏关闭并重新开启,这也是永远不会删除的高分,
有人可以建议一种方法吗?
答案 0 :(得分:5)
使用NSUserDefaults
。
保存:
int score = 12345;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:score] forKey:@"HIGHSCORE"];
加载:
int score = [[[NSUserDefaults standardUserDefaults] objectForKey:@"HIGHSCORE"] integerValue];
答案 1 :(得分:2)
您应该阅读NSUserDefaults
请参阅NSUserDefaults和Preferences and Settings Programming Guide
[[NSUserDefaults standardUserDefaults] setInteger: score forKey: @"Score"]
答案 2 :(得分:0)
在我看来,保存最佳分数的简便方法是将分数保存为字符串值,并在需要时将其记录回来。
//this example assume that high score is in text
NSString *SaveHighScore = HighScore.text;
NSUserDefaults *defaultsHighScore = [NSUserDefaults standardUserDefaults];
[defaultsHighScore setObject:SaveHighScore forKey:@"savedHighScore"];
[defaultsHighScore synchronize];
并召回
NSUserDefaults *defaultsHighScore = [NSUserDefaults standardUserDefaults];
NSString *loadHighScore = [defaultsHighScore objectForKey:@"savedHighScore"];
[HighScore setText :loadHighScore];