实现的功能:1)演示使用属性文件持久化数据。
关键词:数据持久化 属性文件 plist
1、新建一个Sigle View Application,命名为Persistence-file,工程结构如下
[img]
[/img]
2、修改ViewController.xib,添加4个Label控件和4个TextField控件,如下:
[img]
[/img]
3、修改ViewController.h,如下:
#define kFileName @"data.plist"#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property(nonatomic,retain)IBOutlet UITextField *name;@property(nonatomic,retain)IBOutlet UITextField *gender;@property(nonatomic,retain)IBOutlet UITextField *age;@property(nonatomic,retain)IBOutlet UITextField *education;-(NSString *)dataFilePath;-(void)applicationWillResignActive:(NSNotification *)nofication;@end
注意,需要连接各个输出口
4、修改ViewController.m,如下:
#import "ViewController.h"@implementation ViewController@synthesize name,gender,age,education;#pragma mark - View lifecycle- (void)viewDidLoad{ // Do any additional setup after loading the view, typically from a nib. [super viewDidLoad]; UIApplication *app = [UIApplication sharedApplication]; //订阅通知UIApplicationWillResignActiveNotification,进行数据保存操作 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; //初始化数据 [self initData];}-(void)initData{ NSString *filePath = [self dataFilePath]; NSLog(@"filePath=%@",filePath); //从文件中读取数据,首先判断文件是否存在 if([[NSFileManager defaultManager] fileExistsAtPath:filePath]){ NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath]; name.text = [array objectAtIndex:0]; gender.text = [array objectAtIndex:1]; age.text = [array objectAtIndex:2]; education.text = [array objectAtIndex:3]; [array release]; }}-(void)applicationWillResignActive:(NSNotification *)nofication{ NSMutableArray *array = [[NSMutableArray alloc]init]; [array addObject:name.text]; [array addObject:gender.text]; [array addObject:age.text]; [array addObject:education.text]; //将数据写入到文件dataFilePath中 [array writeToFile:[self dataFilePath] atomically:YES]; [array release];}//获得文件路径-(NSString *)dataFilePath{ //检索Documents目录 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);//备注1 NSString *documentsDirectory = [paths objectAtIndex:0];//备注2 return [documentsDirectory stringByAppendingPathComponent:kFileName];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}- (void)viewDidUnload{ [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.name = nil; self.gender = nil; self.age = nil; self.education = nil;}-(void)dealloc{ [name release]; [gender release]; [age release]; [education release];}- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated];}- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated];}- (void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated];}- (void)viewDidDisappear:(BOOL)animated{ [super viewDidDisappear:animated];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}@end
备注1、备注2:
NSDocumentDirectory:常量,表明正在查找Documents目录的路径
NSUserDomainMask:常量,表面将搜索限制与当前应用程序的沙盒中,这样的话只能找到一个Documents目录,因为每个应用程序沙盒中只有一个Documents目录,所以备注2中从paths中取第一个即为当前应用程序的Documents目录的路径
5、编译、运行,在TextField中输入内容,然后退出Simulator,进行测试:
[img]
[/img] [img]
[/img]
6、通过上一篇对IOS应用程序沙盒的的介绍,本工程属性文件data.plist,保存的位置是:
/Users/duobianxing/Library/Application Support/iPhone Simulator/5.0/Applications/88528A36-00CB-4900-9762-1FD5D6AC8595/Documents
[img]
[/img]
7、总结:
1)可以简单保存静态数据,但是无法将自定义对象序列化到属性列表中
2)该持久化方法与上一篇中介绍的NSUserDefaults实质上都是将数据写入到.plist属性文件中,区别是文件保存的位置不同。