使用 NSUserDefaults 保存和加载数据时需要帮助
我正在开发一个新的 iPhone 应用程序,目前我正在尝试使用 NSUserDefaults 保存和加载高分。 我的问题是:我到底应该使用什么代码来将我保存的高分分配给一个名为 highscorespointer1 的变量,该变量是一个指向 NSInteger 的指针? 我目前正在使用如下所示的代码:
这里我正在保存高分。请注意,变量 highscore1 的类型为 NSInteger:
-(IBAction)saveData{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:highscore1 forKey:@"H1"];
[defaults synchronize];
}
这里我正在加载 highscore。但是我收到此警告:“来自不兼容指针类型的赋值”:
highscorepointer1 = [[NSUserDefaults standardUserDefaults] objectForKey:@"H1"];
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"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setInteger:forKey
方法设置一个int
,而不是int *
,这意味着它设置一个值,而不是一个指针。确保您的highscorepointer1
是int
,而不是int *
。无论如何,保存指向整数的指针是没有意义的,但应该保存实际的整数本身。希望有帮助!The
setInteger:forKey
method sets anint
, not anint *
, meaning it sets a value, and not a pointer. Make sure yourhighscorepointer1
is anint
, not anint *
. It doesn't make sense to save the pointer to the integer anyways, but the actual integer itself should be saved. Hope that Helps!