对象归档
2016-02-04 16:17
274 查看
NSString、NSArray、NSData、NSDictionary都实现了NSCoding协议,可直接通过调用writeToFile归档,那么OBJC自定义对象类型呢?首先实现NSCoding协议,重写encodeWithCode方法和initWithCode方法,然后通过NSKeyedArchiver转换为NSData,然后通过NSData的writeToFile方法写入到文件,或者将转换后的NSData放入到NSArray或NSDictionary中调用writeToFile写入到文件便可实现包装了自定义类型的数据和字典的归档;通过NSKeyedUnarchiver读取归档文件到对象,或者通过NSArray的arrrayWithContentsOfFile或NSDictionary的dictionaryWithContentsOfFile到数组对象或字典,然后取出序列化过的自定义对象(即自定义对象的NSData形式),然后通过NSKeyedUnarchiver反归档到对象。
[plain] view
plaincopy
------------------------------------------------测试代码------------------------------------------------
NSString *str = @"hello";
[str writeToFile:@"/Users/lili/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray *arr = [[NSArray alloc]initWithObjects:@"lili",@"tata", nil];
[arr writeToFile:@"/Users/lili/Desktop/arr.txt" atomically:YES];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lili",@"男"] forKeys:@[@"name",@"sex"]];
[dict writeToFile:@"/Users/lili/Desktop/dict.txt" atomically:YES];
Person *parent = [[Person alloc]init];
parent.age = 40;
parent.name = @"lili";
Person *child = [[Person alloc]init];
child.age = 22;
child.name = @"linian";
parent.child = child;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:parent];
[data writeToFile:@"/Users/lili/Desktop/data.txt" atomically:YES];
Person *people = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lili/Desktop/data.txt"];
NSLog(@"%@",people);
NSDictionary *dictPerson = [NSDictionary dictionaryWithObjects:@[[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:child]] forKeys:@[@"parent",@"child"]];
[dictPerson writeToFile:@"/Users/lili/Desktop/dictPerson.txt" atomically:YES];
NSDictionary *pdict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lili/Desktop/dictPerson.txt"];
Person *tPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pdict objectForKey:@"parent"]];
NSLog(@"%@",tPerson);
NSArray *arrPerson = [[NSArray alloc]initWithObjects:[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:parent], nil];
[arrPerson writeToFile:@"/Users/lili/Desktop/arrPerson.txt" atomically:YES];
NSArray *pArr = [NSArray arrayWithContentsOfFile:@"/Users/lili/Desktop/arrPerson.txt"];
Person *aPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pArr objectAtIndex:0]];
NSLog(@"%@",aPerson);
------------------------------------------------Person类------------------------------------------------
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property(strong,nonatomic)Person *child;
@property(assign,nonatomic)int age;
@property(copy,nonatomic)NSString *name;
@end
#import "Person.h"
@implementation Person
-(NSString *)description
{
return [NSString stringWithFormat:@"{age:%d,name:%@,child:%@}",self.age,self.name,self.child];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.child forKey:@"child"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init]){
self.age = [aDecoder decodeIntForKey:@"age"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.child = [aDecoder decodeObjectForKey:@"child"];
}
return self;
}
@end
[plain] view
plaincopy
------------------------------------------------测试代码------------------------------------------------
NSString *str = @"hello";
[str writeToFile:@"/Users/lili/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSArray *arr = [[NSArray alloc]initWithObjects:@"lili",@"tata", nil];
[arr writeToFile:@"/Users/lili/Desktop/arr.txt" atomically:YES];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"lili",@"男"] forKeys:@[@"name",@"sex"]];
[dict writeToFile:@"/Users/lili/Desktop/dict.txt" atomically:YES];
Person *parent = [[Person alloc]init];
parent.age = 40;
parent.name = @"lili";
Person *child = [[Person alloc]init];
child.age = 22;
child.name = @"linian";
parent.child = child;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:parent];
[data writeToFile:@"/Users/lili/Desktop/data.txt" atomically:YES];
Person *people = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/lili/Desktop/data.txt"];
NSLog(@"%@",people);
NSDictionary *dictPerson = [NSDictionary dictionaryWithObjects:@[[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:child]] forKeys:@[@"parent",@"child"]];
[dictPerson writeToFile:@"/Users/lili/Desktop/dictPerson.txt" atomically:YES];
NSDictionary *pdict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/lili/Desktop/dictPerson.txt"];
Person *tPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pdict objectForKey:@"parent"]];
NSLog(@"%@",tPerson);
NSArray *arrPerson = [[NSArray alloc]initWithObjects:[NSKeyedArchiver archivedDataWithRootObject:parent],[NSKeyedArchiver archivedDataWithRootObject:parent], nil];
[arrPerson writeToFile:@"/Users/lili/Desktop/arrPerson.txt" atomically:YES];
NSArray *pArr = [NSArray arrayWithContentsOfFile:@"/Users/lili/Desktop/arrPerson.txt"];
Person *aPerson = [NSKeyedUnarchiver unarchiveObjectWithData:[pArr objectAtIndex:0]];
NSLog(@"%@",aPerson);
------------------------------------------------Person类------------------------------------------------
#import <Foundation/Foundation.h>
@interface Person : NSObject<NSCoding>
@property(strong,nonatomic)Person *child;
@property(assign,nonatomic)int age;
@property(copy,nonatomic)NSString *name;
@end
#import "Person.h"
@implementation Person
-(NSString *)description
{
return [NSString stringWithFormat:@"{age:%d,name:%@,child:%@}",self.age,self.name,self.child];
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.child forKey:@"child"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if(self = [super init]){
self.age = [aDecoder decodeIntForKey:@"age"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.child = [aDecoder decodeObjectForKey:@"child"];
}
return self;
}
@end
相关文章推荐
- 【精】配色达人速成
- 关于iOS中图片的平铺
- 例题6-6 UVs679 Dropping Balls(二叉树)
- Leetcode Path Sum
- %s的用法
- 【Android】3.22 示例22--LBS云检索功能
- jsp相关知识
- 基于Tcp协议与基于Http协议的RPC简介笔记
- 解决pip无法使用http的源
- python 简介
- 蛇形填数
- bootstrap3 ie8
- jackson、Gson反序列化 泛型
- Vim常用命令:移动 跳转 到 文档开头或末尾
- 【IOS自定义工具类之缓存助手类】CacheHelper
- 静态代码块
- Qt核心剖析:信息隐藏
- Win7下Android native code的编译和调试
- 数据结构(2)--线性表单链表的主要操作的实现
- [Redux] Extracting Container Components -- Complete