iOS对象持久化归档 NSKeyedArchiver
来源:互联网 发布:看gv的软件 编辑:程序博客网 时间:2024/05/16 08:01
NSUserDefaults是键值对的形势存在 只能存储包装好的整个数据,
现在我们用NSKeyedArchiver 来存储对象。
首先要在要存储的对象中修改NScoding协议和NScoping协议。
#import <Foundation/Foundation.h>
@interface Student :NSObject<NSCoding,NSCopying>
@property(nonatomic,retain)NSString *Name;
@property(nonatomic,retain)NSString *address;
@property(nonatomic,retain)NSString *gender;
@property(nonatomic,assign)int age;
-(void)encodeWithCoder:(NSCoder *)aCoder
{
//归档数据
[aCoder encodeObject:_NameforKey:@"name"];//_Name 需要改为self.Name要不然回出现内存问题回奔溃的下面一样要改
[aCoderencodeInt:_ageforKey:@"age"];
[aCoder encodeObject:_genderforKey:@"gender"];
[aCoder encodeObject:_addressforKey:@"address"];
}
//解码
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self=[superinit])
{
_Name =[aDecoderdecodeObjectForKey:@"name"];
_age =[aDecoderdecodeIntForKey:@"age"];
_address=[aDecoderdecodeObjectForKey:@"address"];
_gender=[aDecoderdecodeObjectForKey:@"gender"];
}
returnself;
}
//这个方法个人觉的 可以不用写, 一样可以实现
-(id)copyWithZone:(NSZone *)zone
{
Student *stu=[[[selfclass]allocWithZone:zone]init];
stu.Name=[self.NamecopyWithZone:zone];
stu.address=[self.addresscopyWithZone:zone];
stu.gender=[self.gendercopyWithZone:zone];
stu.age=self.age;
return stu;
}
到view界面去添加
-(NSString *)dataFilepath
{ //获取到document路径下的文件
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentDier=[pathsobjectAtIndex:0];
return [documentDierstringByAppendingPathComponent:@"code"];//后缀名 也可以不需要
}
- (void)viewDidLoad
{
[superviewDidLoad];
NSString *path=[selfdataFilepath];
NSFileManager *file=[NSFileManagerdefaultManager];
if([filefileExistsAtPath:path])
{ //判断到有文件 开始解码
NSMutableData *data=[[NSMutableDataalloc]initWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiveralloc]initForReadingWithData:data];
Student *stu=[unarchiverdecodeObjectForKey:@"student"];
[unarchiverfinishDecoding];
self.name.text=stu.Name;
self.age.text=[NSStringstringWithFormat:@"%d",stu.age];
self.address.text=stu.address;
self.gender.text=stu.gender;
[datarelease];
[unarchiverrelease];
}
//程序后台操作的时候程序发送消息通知, 受到通知后调用appear1:这个方法
UIApplication *app=[UIApplicationsharedApplication];
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(appear1:)name:UIApplicationWillResignActiveNotificationobject:app];//object:app 表示必须是系统发送的通知
}
-(void)appear1:(NSNotification *)notification
{
//把界面上text中的内容存到student类中
Student *stu=[[Studentalloc]init];
stu.Name=self.name.text;
stu.age=[self.age.textintValue];
stu.address=self.address.text;
stu.gender=self.gender.text;
// 归档student这个类
NSMutableData *data=[[NSMutableDataalloc]init];
NSKeyedArchiver *archiver=[[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
[archiverencodeObject:stuforKey:@"student"];
[archiverfinishEncoding];
[data writeToFile:[selfdataFilepath]atomically:YES];//gui dan lu jin
[datarelease];
[archiverrelease];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[_namerelease];
[_agerelease];
[_genderrelease];
[_addressrelease];
[superdealloc];
}
- iOS对象持久化归档 NSKeyedArchiver
- IOS数据持久化之归档NSKeyedArchiver
- iOS数据持久化之NSKeyedArchiver(归档)
- iOS数据持久化-归档NSKeyedArchiver
- iOS数据持久化之归档NSKeyedArchiver
- NSKeyedArchiver、NSKeyedUnarchiver 数据持久化归档
- ios ---对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- 【IOS-Swift】实战笔记——数据持久化之归档NSKeyedArchiver
- ios开发学习笔记--数据持久化之归档解档(NSKeyedArchiver/ NSKeyedUnarchiver)
- iOS 对象持久化之- 归档 archiving
- IOS数据持久化之对象归档
- 利用NSKeyedArchiver归档对象
- NSKeyedArchiver (对象归档)
- 数据持久化(三)使用NSKeyedArchiver归档
- OC数据持久化方案2-NSKeyedArchiver归档
- IOS数据持久化之NSKeyedArchiver
- iOS - 数据持久化---- NSUserDefaults & NSKeyedArchiver
- IOS持久化之NSKeyedArchiver,NSKeyedUnarchiver
- JAX-RS入门 一 :基础
- 一个网页控件的呼吸灯效果实例 分享
- 哈希表作用的通俗理解
- ssh2的搭建步骤
- 第一章 初识Mathematica
- iOS对象持久化归档 NSKeyedArchiver
- 优秀的网站评价标准 搜索引擎奈何不了
- Urn-ball Probabilities!
- 【笔记】《通俗详细地讲解什么是P和NP问题》的概念记录
- AAM(Active Appearance Model)算法 理论+实践
- C++中的引用与指针【转】
- 过滤器
- IO - 同步,异步,阻塞,非阻塞 (亡羊补牢篇)
- Probability|Given