为什么NSUserDefaults不能保存我的值?

时间:2022-08-24 16:24:04

Hi I am trying to use NSUserDefaults to save some default values in database. I am able to save the values in the NSUserDefaults (even checked it in NSLog). Now I need the values in app delegate when the application is restarted. But I am not getting anything in the NSUserDefaults. Following is my code from my class where I save the values in NSUserDefaults:

Hi,我尝试使用NSUserDefaults来保存数据库中的一些默认值。我可以在NSUserDefaults中保存值(甚至在NSLog中检查)。现在我需要重新启动应用程序时应用程序委托中的值。但NSUserDefaults中我什么都没有。下面是我的类代码,我将值保存在NSUserDefaults中:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

            [prefs setObject:appDel.dictProfile forKey:@"dict"];
            NSLog(@"%@",[prefs valueForKey:@"dict"]);

Following is my code from App Delegagte:

下面是我来自App Delegagte的代码:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

NSLog(@"%@",[prefs valueForKey:@"dict"]);

the above code always returns me null. Can some one please help me?

上面的代码总是返回null。谁能帮我一个忙吗?

4 个解决方案

#1


197  

If you terminate your app by pressing the home button (in the Simulator or on the device), your NSUserDefaults will get saved.

如果您通过按下home按钮(在模拟器或设备上)来终止应用程序,您的NSUserDefaults将被保存。

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

如果您通过在Xcode(在模拟器或设备上)中按下“Stop”来终止您的应用程序,那么您的NSUserDefaults可能会被保存,但很有可能不会。NSUserDefaults会定期保存任何更改,如果您在这些更改被持久化之前终止它们,它们就会消失。您可以通过调用:

[[NSUserDefaults standardUserDefaults] synchronize];



Addendum:

In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).

在iOS4中(这个答案最初是在iOS3发布的时候写的),在按home键时,NSUserDefaults可能不会被保存。在applicationDidEnterBackground中手动调用[[NSUserDefaults standardUserDefaults]同步]:应该确保您的NSUserDefaults被正确地保存(这实际上应该是一个内置的行为)。

#2


11  

This code works fine for me .

这个代码对我来说很有用。

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:myString forKey:@"Prefs"];
    [standardUserDefaults synchronize];
}

#3


2  

You didn't say whether you are running on a device or in the simulator, but if you restart the application in the simulator, all preferences will be reset between launches if you launch from Xcode. The preferences will only be preserved if you relaunch from the simulator itself.

您没有说您是在设备上运行还是在模拟器中运行,但是如果您在模拟器中重新启动应用程序,如果您从Xcode启动,那么在启动之间将重置所有首选项。只有当您从模拟器本身重新启动时,首选项才会被保留。

#4


1  

In my case I was saving and retrieving a string. When I synchronized after saving and then retrived in another thread, it was not working properly. The problem was solved by synchronizing both after saving and before retreiving.

在我的例子中,我保存并检索一个字符串。当我在保存之后进行同步,然后在另一个线程中检索时,它并没有正常工作。该问题通过保存后同步和还原前同步来解决。

#1


197  

If you terminate your app by pressing the home button (in the Simulator or on the device), your NSUserDefaults will get saved.

如果您通过按下home按钮(在模拟器或设备上)来终止应用程序,您的NSUserDefaults将被保存。

If you terminate your app by pressing "Stop" in Xcode (in the Simulator or on the device), your NSUserDefaults might get saved, but there's a good chance they won't. NSUserDefaults persists any changes periodically, and if you terminate the process before they've been persisted, they'll be gone. You can force the save by calling:

如果您通过在Xcode(在模拟器或设备上)中按下“Stop”来终止您的应用程序,那么您的NSUserDefaults可能会被保存,但很有可能不会。NSUserDefaults会定期保存任何更改,如果您在这些更改被持久化之前终止它们,它们就会消失。您可以通过调用:

[[NSUserDefaults standardUserDefaults] synchronize];



Addendum:

In iOS4 (this answer was originally written when iOS3 was the public release), your NSUserDefaults may not get saved when pressing the home button. Manually calling [[NSUserDefaults standardUserDefaults] synchronize] in applicationDidEnterBackground: should ensure that your NSUserDefaults are saved correctly (this should really be a built-in behaviour IMO).

在iOS4中(这个答案最初是在iOS3发布的时候写的),在按home键时,NSUserDefaults可能不会被保存。在applicationDidEnterBackground中手动调用[[NSUserDefaults standardUserDefaults]同步]:应该确保您的NSUserDefaults被正确地保存(这实际上应该是一个内置的行为)。

#2


11  

This code works fine for me .

这个代码对我来说很有用。

NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];

if (standardUserDefaults) {
    [standardUserDefaults setObject:myString forKey:@"Prefs"];
    [standardUserDefaults synchronize];
}

#3


2  

You didn't say whether you are running on a device or in the simulator, but if you restart the application in the simulator, all preferences will be reset between launches if you launch from Xcode. The preferences will only be preserved if you relaunch from the simulator itself.

您没有说您是在设备上运行还是在模拟器中运行,但是如果您在模拟器中重新启动应用程序,如果您从Xcode启动,那么在启动之间将重置所有首选项。只有当您从模拟器本身重新启动时,首选项才会被保留。

#4


1  

In my case I was saving and retrieving a string. When I synchronized after saving and then retrived in another thread, it was not working properly. The problem was solved by synchronizing both after saving and before retreiving.

在我的例子中,我保存并检索一个字符串。当我在保存之后进行同步,然后在另一个线程中检索时,它并没有正常工作。该问题通过保存后同步和还原前同步来解决。