xcode 应用程序关闭后如何保存变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6671556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save variables after application shut down?
提问by vburojevic
I want to save some integers after application shut down and restore them after application opening, what is the easiest way to do this?
我想在应用程序关闭后保存一些整数并在应用程序打开后恢复它们,最简单的方法是什么?
回答by Zeppomedio
You should store and load data from NSUserDefaults:
您应该从 NSUserDefaults 存储和加载数据:
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];
回答by Noah Witherspoon
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 -synchronizeat some point before your app terminates) will persist between launches.
查看NSUserDefaults 文档。您可以在那里设置任意键值对(只要您-synchronize在应用程序终止之前的某个时刻调用共享用户默认对象)将在启动之间保持不变。
回答by Brandon Schlenker
You can save them in the NSUserDefaults. This is mainly used for preferences.
您可以将它们保存在 NSUserDefaults 中。这主要用于首选项。
[[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
回答by Hollance
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.
研究使用 NSUserDefaults。这就像一个字典,您可以向其中添加键/值对。您将变量保存在应用程序委托的 applicationWillTerminate 和 applicationDidEnterBackground 方法中。在 applicationDidFinishLoading 中再次加载变量。
回答by Caleb
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.
最简单的方法是使用 NSUserDefaults。-applicationWillTerminate:当应用程序即将关闭时,您的应用程序委托会收到一条消息,您可以将数据写入 NSUserDefaults(如果数据量很大,则将其写入您自己的文件)。然后,当您的应用程序再次启动时,您的应用程序委托将获得一个-applicationDidFinishLaunching,您可以再次读取您的数据。
回答by hungryMind
Serialize them and store them on memory. You have to do this before shut down and load when app is reopened
将它们序列化并将它们存储在内存中。您必须在关闭并在重新打开应用程序时加载之前执行此操作

