iPhone开发数据持久化总结之第2篇—属性文件(.plist) .
实现的功能: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
#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