0

I'm using NSUserDefaults to hold some data while my app is running, but I want to dump all the data on app termination. Is this possible, and if so how do I do this? Thanks! ~Carpetfizz I know that there's this, but I'm not sure where to put it. Should it go in the app delegate?

    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];



- (void)applicationWillTerminate:(UIApplication *)application
{


   // NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
    //[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];

    [NSUserDefaults resetStandardUserDefaults];





}
4

2 回答 2

1

你正在做的是hacky,这对应用程序的可维护性来说是个坏主意:-)。NSUserDefaults 用于持久存储(跨应用程序启动)。您将自己困在一条无法使用 NSUserDefaults 存储持久设置的路径中。您应该考虑创建一个新类,其中包含您要填充到 NSUserDefaults 中的“临时”信息。

尝试这样的事情:

@interface MyAppSettings : NSObject
@property (nonatomic, strong) NSString *userName;
@property (nonatomic, strong) NSString *authenticationToken;
@end

@implementation MyAppSettings
@end
于 2013-06-02T18:59:55.053 回答
1

您的问题的答案是:您应该使用NSUserDefaults' 类方法:

+ (void)resetStandardUserDefaults

关于ApplicationDelegate's 方法:

- (void)applicationWillTerminate:(UIApplication *)application

NSUserDefaults但一个更好的问题是:如果你想在应用程序完成执行后转储你存储的所有信息,你为什么要使用?最好将所有信息存储在一个对象(可能是单例?)上,而不是使用NSUserDefaults.

于 2013-06-02T18:02:10.017 回答