NSUserDefaults 不保存 [英] NSUserDefaults not saving

查看:21
本文介绍了NSUserDefaults 不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 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"]);

但是当我停止程序并再次运行它时,它总是只显示难度:简单.有什么建议吗?

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:/code>,如果存在,它将返回内存中的值,如果不存在,则查找已保存到长期存储中的内容.

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 不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆