iPhone开发【二十二】数据持久化总结之第3篇—归档(NSKeyedArchiver、NSKeyedUnarchiver)
Person.m: 3、接下来主要修改ViewController 主要修改了ViewController.m,如下: 4、通过 可以知道保存数据archive文件的存储位置是:
发布日期:2021-09-28 18:46:35
浏览次数:10
分类:技术文章
本文共 5762 字,大约阅读时间需要 19 分钟。
转载请注明出处,原文网址: 作者:张燕广
实现的功能:1)演示使用归档持久化数据。
关键词:数据持久化 归档 NSKeyedArchiver NSKeyedUnarchiver
1、将上一篇的工程拷贝一份,名称修改为Persistence-archiver,工程结构如下:
2、添加Person.h类,如下:
Person.h:
- #import <Foundation/Foundation.h>
- @interface Person : NSObject<NSCoding,NSCopying>
- @property(nonatomic,retain)NSString *name;
- @property(nonatomic,retain)NSString *gender;
- @property(nonatomic,retain)NSString *age;
- @property(nonatomic,retain)NSString *education;
- @end
- #import "Person.h"
- #define kNameKey @"name"
- #define kGenderKey @"gender"
- #define kAgeKey @"age"
- #define kEducationKey @"education"
- @implementation Person
- @synthesize name,gender,age,education;
- #pragma mark -
- #pragma mark NSCoding
- -(void)encodeWithCoder:(NSCoder *)aCoder{ //编码
- [aCoder encodeObject:name forKey:kNameKey];
- [aCoder encodeObject:gender forKey:kGenderKey];
- [aCoder encodeObject:age forKey:kAgeKey];
- [aCoder encodeObject:education forKey:kEducationKey];
- }
- -(id)initWithCoder:(NSCoder *)aDecoder{ //解码
- if(self == [super init]){
- name = [[aDecoder decodeObjectForKey:kNameKey]retain];
- gender = [[aDecoder decodeObjectForKey:kGenderKey]retain];
- age = [[aDecoder decodeObjectForKey:kAgeKey]retain];
- education = [[aDecoder decodeObjectForKey:kEducationKey]retain];
- }
- return self;
- }
- #pragma mark -
- #pragma mark NSCopying
- -(id)copyWithZone:(NSZone *)zone{
- Person *person = [[[self class]allocWithZone:zone]init];
- person.name = [[self.name copyWithZone:zone]autorelease];
- person.gender = [[self.gender copyWithZone:zone]autorelease];
- person.name = [[self.name copyWithZone:zone]autorelease];
- person.name = [[self.name copyWithZone:zone]autorelease];
- return person;
- }
- @end
ViewController.h,主要是修改了宏,如下:
- <span style="font-size:18px;">//#define kFileName @"data.plist"
- #define kFileName @"archive"
- #define kDataKey @"Data"
- #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</span>
- <span style="font-size:18px;">#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</span>
/Users/duobianxing/Library/Application Support/iPhone Simulator/5.0/Applications/F694104D-894D-4230-A01B-C62066B3DEC8/Documents
5、总结:
与属性列表相比,归档可以写入复杂的对象(Person类的实例)。
需要源码的网友请留言哦
转载地址:https://blog.csdn.net/h3c4lenovo/article/details/8568227 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年04月17日 02时54分11秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
FTP文件管理项目(本地云)项目日报(八)
2019-04-27
【Linux】血泪教训 -- 动态链接库配置方法
2019-04-27
FTP文件管理项目(本地云)项目日报(九)
2019-04-27
以练代学设计模式 -- FTP文件管理项目
2019-04-27
FTP文件管理项目(本地云)项目日报(十)
2019-04-27
学以致用设计模式 之 “组合模式”
2019-04-27
我用过的设计模式(7)--享元模式
2019-04-27
MySQL数据库从入门到实战应用(学习笔记一)
2019-04-27
MySQL数据库从入门到实战应用(学习笔记二)
2019-04-27
种树:二叉树、二叉搜索树、AVL树、红黑树、哈夫曼树、B树、树与森林
2019-04-27
【C++】攻克哈希表(unordered_map)
2019-04-27
转:【答学员问】- 该如何根据岗位学习相关技能
2019-04-27
转:【答学员问】有什么经验教训,是你在面试很多次之后才知道的?
2019-04-27
消息队列:解耦、异步、削峰,现有MQ对比以及新手入门该如何选择MQ?
2019-04-27
【奇技淫巧】-- 三角形最小路径和
2019-04-27
【小技巧】argc和argv的用法
2019-04-27
学不下去了怎么办?
2019-04-27
二叉树的前中后序遍历(迭代法)(带动画)
2019-04-27
【小技巧】【XShell】【Xftp】Windows桌面与Linux虚拟机互传文件
2019-04-27
【redis入门】Centos下安装redis
2019-04-27