NSUserDefaults 不会永久保存
我正在制作一个相当简单的 iPhone 应用程序。但是,我无法永久保存我的 NSUserDefaults 。写入和检索数据不是问题——我可以保存数据(在我的例子中是一个字符串),并根据命令检索它,即使在切换视图、关闭/打开应用程序等之后,也可以检索数据美好的。看起来好像字符串已正确保存到密钥中。但是,当我从多任务托盘中退出应用程序并重新启动它时,设置不再保存,并且应用程序像第一次一样启动。我是一名新手程序员,所以这可能只是我的一个愚蠢的错误。
保存的样子如下:
if (optionsSoundBox.center.x >= 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 1;
self.soundOffLabel.alpha = 0;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsON" forKey:@"SoundKey"];
[soundOptions synchronize];
}
if (optionsSoundBox.center.x < 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 0;
self.soundOffLabel.alpha = 1;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsOFF" forKey:@"SoundKey"];
[soundOptions synchronize];
}
我在 viewDidLoad 中检索字符串,以便它在启动时准备就绪,如下所示:
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
NSString *savedSoundSettings = [soundOptions stringForKey:@"SoundKey"];
if (savedSoundSettings == @"SoundsON")
{
[optionsSoundBox setCenter:CGPointMake(280, optionsSoundBox.center.y)];
}
if (savedSoundSettings == @"SoundsOFF")
{
[optionsSoundBox setCenter:CGPointMake(200, optionsSoundBox.center.y)];
}
我真的很感谢您提供的任何帮助
I have a fairly simple iPhone application that I am making. However, I cannot get my NSUserDefaults to save permanently. Writing and retrieving the data is not a problem––I can save the data (in my case, a string), and retrieve it on command, even after switching views, closing/opening the application, etc, and the data is retrieved just fine. It appears as if the string was saved properly to the key. But when I quit the application from the multitasking tray and restart it, the settings are no longer saved, and the application boots up like it is the first time. I am a novice programmer, so it is probably just a stupid error on my part.
Here's what the saving looks like:
if (optionsSoundBox.center.x >= 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 1;
self.soundOffLabel.alpha = 0;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsON" forKey:@"SoundKey"];
[soundOptions synchronize];
}
if (optionsSoundBox.center.x < 240)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.2];
self.soundOnLabel.alpha = 0;
self.soundOffLabel.alpha = 1;
[UIView commitAnimations];
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
[soundOptions setObject:@"SoundsOFF" forKey:@"SoundKey"];
[soundOptions synchronize];
}
I retrieve the string in the viewDidLoad so it will be ready on startup like this:
NSUserDefaults *soundOptions = [NSUserDefaults standardUserDefaults];
NSString *savedSoundSettings = [soundOptions stringForKey:@"SoundKey"];
if (savedSoundSettings == @"SoundsON")
{
[optionsSoundBox setCenter:CGPointMake(280, optionsSoundBox.center.y)];
}
if (savedSoundSettings == @"SoundsOFF")
{
[optionsSoundBox setCenter:CGPointMake(200, optionsSoundBox.center.y)];
}
I really appreciate any help you can give
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而且因为您得到的是
stringForKey:
而不是objectForKey:
如果您只使用
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: 也许对您来说会更容易@"SoundsON"];
然后检查boolForKey: @"SoundsON"
是否为 true。And also because you're getting
stringForKey:
insteadobjectForKey:
Maybe it would be easier for you if you just used
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: @"SoundsON"];
and then check ifboolForKey: @"SoundsON"
is true.您应该使用
[savedSoundSettings isEqualToString:@"SoundsON"]
,而不是使用双等于运算符。 (<代码>==)。除此之外,可能是您在
viewDidLoad
内部运行此代码。尝试在viewWillAppear
内部运行它。最后,我建议使用驼峰命名法作为设置键,这样从长远来看打字会更容易。 (想想
soundsOn
而不是SoundsON
。)You should use
[savedSoundSettings isEqualToString:@"SoundsON"]
, instead of using the double equals operator. (==
).Aside from that, it could be the fact that you are running this code inside of
viewDidLoad
. Try to run it inside ofviewWillAppear
instead.Finally, I recommend using camelcase names for settings keys, so it will be easier to type in the long run. (Think
soundsOn
instead ofSoundsON
.)您不应将字符串与
==
进行比较。比较字符串的正确方法是通过isEqualToString:
。因此,您在检索用户默认值时的 if 语句应如下所示:编辑:此外,从概念上讲,您要检查的只是单个状态变量是 ON 还是 OFF。因此,理想情况下,您应该使用
[NSUserDefaults setBool:forKey:]
和[NSUserDefaults boolForKey:]
之类的东西。You should not compare strings with
==
. The correct way to compare strings is viaisEqualToString:
. So your if statements while retrieving the user defaults should look like:EDIT: Moreover, conceptually, all you are checking is whether a single state variable is ON or OFF. So you should ideally be using something like
[NSUserDefaults setBool:forKey:]
and[NSUserDefaults boolForKey:]
.