iPhone开发数据持久化总结之第3篇—归档(NSKeyedArchiver、NSKeyedUnarchiver) .
实现的功能:1)演示使用归档持久化数据。
关键词:数据持久化 归档 NSKeyedArchiver NSKeyedUnarchiver
1、将上一篇iPhone开发数据持久化总结之第2篇属性文件(.plist)的工程拷贝一份,名称修改为Persistence-archiver,工程结构如下:
[img]
[/img]
2、添加Person.h类,如下:
Person.h:
#import "ViewController.h"#import "Person.h"@implementation ViewController@synthesize name,gender,age,education;-(NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingPathComponent:kFileName];}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}#pragma mark - View lifecycle- (void)viewDidLoad{// Do any additional setup after loading the view, typically from a nib. 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];*/ NSData *data = [[NSMutableData alloc]initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data]; Person *person = [unarchiver decodeObjectForKey:kDataKey]; [unarchiver finishDecoding]; name.text = person.name; gender.text = person.gender; age.text = person.age; education.text = person.education; [unarchiver release]; [data release]; } UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app]; [super viewDidLoad];}-(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]; [array writeToFile:[self dataFilePath] atomically:YES]; [array release];*/ Person *person = [[Person alloc]init]; person.name = name.text; person.gender = gender.text; person.age = age.text; person.education = education.text; NSMutableData *data = [[NSMutableData alloc]init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data]; [archiver encodeObject:person forKey:kDataKey]; [archiver finishEncoding]; [data writeToFile:[self dataFilePath] atomically:YES]; [person release]; [archiver release]; [data release];}- (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