应用程序关闭后如何保存变量?

发布于 2024-11-19 21:02:19 字数 47 浏览 2 评论 0 原文

我想在应用程序关闭后保存一些整数并在应用程序打开后恢复它们,最简单的方法是什么?

I want to save some integers after application shut down and restore them after application opening, what is the easiest way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乖乖 2024-11-26 21:02:20

您可以将它们保存在 NSUserDefaults 中。这主要用于偏好。

[[NSUserDefaults standardUserDefaults] setObject:someInteger forKey:@"someIntegerKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

如果您想要存储更多数据,您还可以将它们保存到属性列表文件中。

NSDictionary *someDictionary = [NSDictionary dictionaryWithObjectsAndKeys:someInt1, @"someIntKey1", someInt2, @"someIntKey2", nil];

[someDictionary writeToFile:somePath error:&error];

要在退出应用程序时保存,请将任何代码放入

- (void)applicationWillTerminate:(UIApplication *)application

You can save them in the NSUserDefaults. This is mainly used for preferences.

[[NSUserDefaults standardUserDefaults] setObject:someInteger forKey:@"someIntegerKey"];
[[NSUserDefaults standardUserDefaults] synchronize];

You can also save them to a Property List file if you have more data you'd like to store.

NSDictionary *someDictionary = [NSDictionary dictionaryWithObjectsAndKeys:someInt1, @"someIntKey1", someInt2, @"someIntKey2", nil];

[someDictionary writeToFile:somePath error:&error];

To save upon exiting the app place any code in

- (void)applicationWillTerminate:(UIApplication *)application
你列表最软的妹 2024-11-26 21:02:20

考虑使用 NSUserDefaults。这就像一个字典,您可以向其中添加键/值对。您将变量保存在应用程序委托的 applicationWillTerminate 和 applicationDidEnterBackground 方法中。您在 applicationDidFinishLoading 中再次加载变量。

Look into using NSUserDefaults. This works like a dictionary that you can add key/value pairs to. You save the variables in your app delegate's applicationWillTerminate and applicationDidEnterBackground methods. You load the variables again in applicationDidFinishLoading.

南城追梦 2024-11-26 21:02:20

最简单的方法是使用 NSUserDefaults。当应用程序即将关闭时,您的应用程序委托将收到一条 -applicationWillTerminate: 消息,您可以将数据写入 NSUserDefaults (如果数据量很大,则将其写入您自己的文件中) 。然后,当您的应用再次启动时,您的应用委托将获得 -applicationDidFinishLaunching,您可以再次读回数据。

The easiest way is to use NSUserDefaults. Your app delegate will get an -applicationWillTerminate: message when the app is about to shut down, and you can write your data to NSUserDefaults (or write it into your own file if the amount of data is large). Then, when your app starts up again, your app delegate will get an -applicationDidFinishLaunching, and you can read your data back again.

错爱 2024-11-26 21:02:20

将它们序列化并将它们存储在内存中。您必须在关闭并重新打开应用程序时加载之前执行此操作

Serialize them and store them on memory. You have to do this before shut down and load when app is reopened

划一舟意中人 2024-11-26 21:02:19

您应该从 NSUserDefaults 存储和加载数据:

http://developer.apple.com/library/IOS/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// to store
[defaults setObject:[NSNumber numberWithInt:12345] forKey:@"myKey"];
[defaults synchronize];

// to load
NSNumber *aNumber = [defaults objectForKey:@"myKey"];
NSInteger anInt = [aNumber intValue];

You should store and load data from NSUserDefaults:

http://developer.apple.com/library/IOS/#documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/Reference/Reference.html

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// to store
[defaults setObject:[NSNumber numberWithInt:12345] forKey:@"myKey"];
[defaults synchronize];

// to load
NSNumber *aNumber = [defaults objectForKey:@"myKey"];
NSInteger anInt = [aNumber intValue];
荆棘i 2024-11-26 21:02:19

查看NSUserDefaults 文档。您可以在那里设置任意键值对(只要您在应用程序终止之前的某个时刻调用共享用户默认对象的 -synchronize )将在启动之间持续存在。

Check out the NSUserDefaults documentation. You can set arbitrary key-value pairs there which (as long as you call the shared user defaults object’s -synchronize at some point before your app terminates) will persist between launches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文