问题描述
我的Sprite Kit应用程序出现问题,我的NSUserDefaults
变量不起作用.在createSceneContents
(我知道被称为)
I am having a problem in my sprite kit app where my NSUserDefaults
variable is not working. In createSceneContents
(which I know is being called)
if (![defaults objectForKey:@"obj"]) {
difficultyLabel.text = @"Difficulty: Easy";
[defaults setObject:@"Easy" forKey:@"obj"];
} else {
difficultyLabel.text = [NSString stringWithFormat:@"Difficulty: %@", [defaults objectForKey:@"diff"]];
}
,然后单击SKLabelNode
更改难度,此代码即被调用
and then when you click on the SKLabelNode
to change the difficulty and this code is being called
if ([label.text isEqualToString:@"Difficulty: Easy"]) {
label.text = @"Difficulty: Hard";
[defaults setObject:@"Hard" forKey:@"obj"];
NSLog(@"%@",[defaults objectForKey:@"obj"]);
} else {
label.text = @"Difficulty: Easy";
[defaults setObject:@"Easy" forKey:@"obj"];
NSLog(@"%@",[defaults objectForKey:@"obj"]);
但是当我停止程序并再次运行它时,它总是只说Difficulty:容易.有什么建议吗?
but when I stop the program and run it again, it always just says Difficulty: Easy. Any suggestions?
推荐答案
正如rmaddy的回答所解释的,NSUserDefaults
不会立即将数据写入长期存储.
As rmaddy's answer explains, NSUserDefaults
won't immediately write data to long-term storage.
当您调用setObject:forKey:
时,这些值将保存在临时内存中,因此,如果您尝试在setObject:forKey:
之后调用objectForKey:
,它将在内存中返回该值;如果不存在,它将返回到寻找已保存到长期存储中的内容.
The values are saved in temporary memory when you call setObject:forKey:
, so if you ever try to call objectForKey:
after setObject:forKey:
, it will return the value in memory if it exists, and if not, it goes to look for what's been saved to long-term storage.
在后台,设备最终会将这些值保存到永久存储中.这是操作系统要处理的事情,但是在您的应用正常运行时,多数时候不必立即将这些值存储到永久存储中,因此,让操作系统在经过优化后可以执行此操作(我想象一下,操作系统可能会按一定的时间表立即同步每个应用程序的用户默认值,并尽可能在处理器空闲时尝试执行此操作.)
In the background, the device will eventually save these values to permanent storage. It's something the OS handles, but in normal operation of your app, it shouldn't be necessary to immediately store these values to permanent storage most of the time, so let the OS do this at times when its been optimized to do so (I imagine the OS probably synchs every app's user defaults at once on some regular schedule, and as much as possible, tries to do this when the processor is idle probably).
但是,正如rmaddy所解释的那样,如果您有一些急需立即保存到永久存储中的内容,则可以随时调用synchronize
.不过请记住...只要在调试模式下不会杀死您的应用,就会存储您设置为存储在用户默认值中的值.
But with that said, as rmaddy explains, if you've got something that crucially needs to be saved to the permanent storage immediately, you can always call synchronize
. Keep in mind though... as long as your app isn't killed while in debug mode, the values you've set to be stored in user defaults will be stored.
但是出于您自己的考虑,如果您想绝对确定,可以按照rmaddy的建议在applicationDidEnterBackground
中将其称为对synchronize
的调用.不过请记住,如果您杀死应用程序,则不会调用此方法.
But for your own sake, if you want to be absolutely certain, you can put it a call to synchronize
in applicationDidEnterBackground
as rmaddy suggests. Keep in mind though, this method isn't called if you kill the app.
这篇关于NSUserDefaults不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!