第一種方式User Defaults。
實現(xiàn)的功能:1)演示NSUserDefaults持久化數(shù)據。
關鍵詞:數(shù)據持久化 NSUserDefaults
1、新建一個Sigle View Application,命名為Persistence_NSUserDefaults,工程結構如下:

2、修改ViewController.xib,添加兩個Label控件和兩個TextField控件,如下:

3、修改ViewController.h,如下:
[cpp]view plaincopy
#import?
@interface?ViewController?:?UIViewController
@property(retain,nonatomic)IBOutlet?UITextField?*server;
@property(retain,nonatomic)IBOutlet?UITextField?*port;
@end
連接輸出口server、port,如下:

4、修改ViewController.m,如下:
[cpp]view plaincopy
#import?"ViewController.h"
@interface?ViewController?()
@end
@implementation?ViewController
@synthesize?server;
@synthesize?port;
-?(void)viewDidLoad
{
[super?viewDidLoad];
//?Do?any?additional?setup?after?loading?the?view,?typically?from?a?nib.
NSLog(@"viewDidLoad");
//初始化數(shù)據
[self?initData];
//訂閱通知UIApplicationDidEnterBackgroundNotification,進行數(shù)據保存操作
UIApplication?*app?=?[UIApplication?sharedApplication];
[[NSNotificationCenter?defaultCenter]addObserver:self?selector:@selector(applicationWillDidEnterBackground:)?name:UIApplicationDidEnterBackgroundNotification?object:app];
}
//初始化數(shù)據
-(void)initData{
NSUserDefaults?*defaults?=?[NSUserDefaults?standardUserDefaults];
server.text?=??[defaults?objectForKey:@"server"];
port.text?=?[defaults?objectForKey:@"port"];
}
-(void)applicationWillDidEnterBackground:(NSNotification?*)notification{
NSLog(@"#applicationWillEnterForeground");
[self?saveData];
}
//保存數(shù)據
-(void)saveData{
NSUserDefaults?*defaults?=?[NSUserDefaults?standardUserDefaults];
[defaults?setObject:server.text?forKey:@"server"];
[defaults?setObject:port.text?forKey:@"port"];
[defaults?synchronize];//強制User?Defaults系統(tǒng)進行保存
}
-?(void)viewDidUnload
{
[super?viewDidUnload];
//?Release?any?retained?subviews?of?the?main?view.
server?=?nil;
port?=?nil;
}
-?(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return(interfaceOrientation?!=?UIInterfaceOrientationPortraitUpsideDown);
}
@end
5、編譯、運行,在TextField中輸入如下內容:

退出Simulator,然后重新運行程序,上次輸入的內容已顯示在TextField中。
6、數(shù)據到底保存到哪兒了???
iPhone應用程序沙盒,或許你聽過這個東東,沒聽過也沒關系,下面就說說它。
1)iPhone應用程序只能在為該改程序創(chuàng)建的文件系統(tǒng)中讀取文件,不可以去其它地方訪問,此區(qū)域被成為沙盒。
2)? 沙盒在哪兒呢?先來看一下iPhone應用程序安裝后的存放位置,我mac上的地址如下:
/Users/duobianxing/Library/Application Support/iPhone Simulator/5.0
我的模擬器是5.0的,截圖如下:

打開Applications目錄,截圖如下:
上圖中每個目錄都是一個應用程序的沙盒,最上面的那個目錄就是剛剛演示的工程Persistence_NSUserDefaults安裝后的目錄,打開該目錄,截圖如下:

也可以打開Applications下的其它目錄驗證一下,每個iPhone應用程序自己的沙盒下有3個目錄,分別是:
1)Documents:應用程序數(shù)據保存到該目錄下,但是基于NSUserDefaults的數(shù)據不會保存到該目錄下(所以,剛才演示的工程Persistence_NSUserDefaults的數(shù)據沒有保存到該目錄下)
2)Library:基于NSUserDefaults的數(shù)據會保存到該目錄,工程Persistence_NSUserDefaults的數(shù)據一定保存到該目錄下啦,趕緊找一下吧,
子目錄Preferences下的com.zyg.ios.Persistence-NSUserDefaults.plist文件保存了server、port數(shù)據,打開看一下,截圖如下:

可以發(fā)現(xiàn),使用NSUserDefaults時,數(shù)據默認保存在沙盒的Library目錄下的文件:工程名稱.plist中。
3)tmp:存儲臨時文件。每個應用程序應該負責刪除自己tmp目錄下的臨時數(shù)據。
7、總結:
NSUserDefaults一般用于保存首選項信息、緩存數(shù)據等少量數(shù)據。
本文工程Persistence_NSUserDefaults純粹為了演示,實際開發(fā)中不一定這樣應用。