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

问题描述:

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

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

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

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];