//
// main.m
// YuFaJinJie
//
// Created by 千雅爸爸 on 16/10/6.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
//归档和解档achiver unachiver,数据持久化的方式,
//一种是plist文件一种是nsuserdefault,这两种都是操作的系统给的类型,例如字典和数组
//现在有一种可以自定义的类型的存储,这个是不是有点像java的serialization?
//在相应的类中,要进行归档必须要实现协议nscoding,正式的协议就是java里面的接口,和java里面要实现序列化就要实现serilization的接口一样,这里要实现nscoding
//@interface Person : NSObject <NSCoding>
void guidanjiedang(){
Person *person = [[Person alloc] init];
person.name = @"Tom";
person.age = 20;
person.sex = @"male";
//归档器的类叫做NSKeyedArchiver,里面有一个类方法
NSData *personData = [NSKeyedArchiver archivedDataWithRootObject:person];
NSLog(@"二进制:%@",personData);
//写入到文件中
[personData writeToFile:@"/Users/JiYi2013/Desktop/guidang" atomically:YES];
//从文件中读取,二进制,然后转化为类
personData = [NSData dataWithContentsOfFile:@"/Users/JiYi2013/Desktop/guidang"];
Person *unachiverperson2 = [NSKeyedUnarchiver unarchiveObjectWithData:personData];
NSLog(@"unachiver解档:%@",unachiverperson2);
//多个类型的归档和解档
Person *person2 = [[Person alloc] init];
person2.name =@"java";
person2.age = 20;
person2.sex = @"null";
NSMutableData *mutabledata = [NSMutableData data];
NSKeyedArchiver *personMutableAchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mutabledata];
[personMutableAchiver encodeObject:person forKey:@"person"];
[personMutableAchiver encodeObject:person2 forKey:@"person2"];
//一定要结束归档,不然会一只归档
[personMutableAchiver finishEncoding];
NSLog(@"%@",mutabledata);
//多个类型的解档
NSKeyedUnarchiver *personMutableUnachiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:mutabledata];
Person *decodeperson = [personMutableUnachiver decodeObjectForKey:@"person"];
Person *decodeperson2 = [personMutableUnachiver decodeObjectForKey:@"person2"];
NSLog(@"解档:反序列化:person:%@",decodeperson);
NSLog(@"解档:反序列化:person2:%@",decodeperson2);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
guidanjiedang();
}
return 0;
}
person:
//
// Person.h
// YuFaJinJie
//
// Created by 千雅爸爸 on 16/10/6.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import <Foundation/Foundation.h>
//要进行归档必须要实现协议nscoding,正式的协议就是java里面的接口,和java里面要实现序列化就要实现serilization的接口一样,这里要实现nscoding
@interface Person : NSObject <NSCoding>
//NSData 是oc里面存储二进制的,
@property(nonatomic,copy) NSString *name;
@property(nonatomic,assign) NSInteger age;
@property(nonatomic,copy) NSString *sex;
@end
//
// Person.m
// YuFaJinJie
//
// Created by 千雅爸爸 on 16/10/6.
// Copyright © 2016年 kodulf. All rights reserved.
//
#import "Person.h"
@implementation Person
//归档achiver的时候,也就是序列化的时候,会自动的调用这个方法
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:_name forKey:@"name"];
[coder encodeInteger:_age forKey:@"age"];
[coder encodeObject:_sex forKey:@"sex"];
}
//接档unachiver
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super init];
if(self){
_name = [coder decodeObjectForKey:@"name"];
_age = [coder decodeIntegerForKey:@"age"];
_sex = [coder decodeObjectForKey:@"sex"];
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name=%@,age=%ld,sex=%@", _name,_age,_sex];
}
@end