// Student.h
#import <Foundation/Foundation.h>
@interface Student : NSObject <NSCoding>
@property (nonatomic,assign) int idNum;
@property (nonatomic,copy) NSString *name;
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end
// Student.m
#import "Student.h"
#define IDNUM @"idNum"
#define kName @"name"
@implementation Student
#pragma mark 编码 对对象属性进行编码的处理
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeInt:_idNum forKey:IDNUM];
[aCoder encodeObject:_name forKey:kName];
}
#pragma mark 解码 解码归档数据来初始化对象
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self == [super init]) {
_idNum = [aDecoder decodeIntegerForKey:IDNUM];
_name = [aDecoder decodeObjectForKey:kName];
}
return self;
}
- (NSString*)description {
return [NSString stringWithFormat:@"idNum = %i name = %@",_idNum,_name];
}
@end
<pre name="code" class="objc">// ViewController.m
// 归档
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@end
@implementation ViewController
#pragma mark 复习
- (void)plistAndUserDefaultRevise {
//动态创建一个plist文件
//得到路径
//属性列表(存到document目录里面)
// NSDictionary *dic = @{@"idNum":[NSNumber numberWithInt:1],@"name":@"bendan"};
// if ([dic writeToFile:filePath atomically:YES]) {
// NSLog(@"存储成功........save ok!");
// }
//userdefault(存到library里面)
// NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// [defaults setObject:@"benJiDan" forKey:@"name"];
// [defaults synchronize];
}
#pragma mark 自定义内容归档
- (void)archvierByMyself {
//自定义内容归档
NSString *filePathTwo = [self getDocumentAppendingPathByFileName:@"filePathTwo.archiver"];
//1. 使用NSData存放归档数据
NSMutableData *data = [NSMutableData data];
//2. 创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//3. 添加归档内容 (设置键值对)
[archiver encodeObject:@"jereh" forKey:@"name"];
[archiver encodeInt:20 forKey:@"age"];
[archiver encodeObject:@[@"benDan",@"daBenDan"] forKey:@"zhangziyao"];
//4. 完成归档
[archiver finishEncoding];
//5. 将归档信息存储到磁盘上
if ([data writeToFile:filePathTwo atomically:YES]) {
NSLog(@"存储笨蛋完毕>>.....<<");
}
//=========================================================
//解归档
//1. 从磁盘读取文件,生成NSData实例
NSData *unarchiverData = [NSData dataWithContentsOfFile:filePathTwo];
//2. 根据Data实例创建和初始化解归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unarchiverData];
//3. 解归档,根据key值访问
NSString *name = [unarchiver decodeObjectForKey:@"name"];
int age = [unarchiver decodeIntegerForKey:@"age"];
NSArray *nameAry = [unarchiver decodeObjectForKey:@"zhangziyao"];
NSLog(@"name = %@ ,age = %i , nameAry = %@ ",name,age,nameAry);
}
#pragma mark 归档对象
- (void)archiverModel {
NSString *studentPath = [self getDocumentAppendingPathByFileName:@"student.archiver"];
//1. 使用NSData存放归档数据
NSMutableData *data = [NSMutableData data];
//2. 创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//3. 添加归档内容
Student *stu1 = [Student new];
stu1.idNum = 1;
stu1.name = @"shaGua";
Student *stu2 = [Student new];
stu2.idNum = 2;
stu2.name = @"小青年";
[archiver encodeObject:stu1 forKey:@"studentOne"];
[archiver encodeObject:stu2 forKey:@"studentTwo"];
//4. 完成归档
[archiver finishEncoding];
//5. 将归档信息存储到磁盘上
if ([data writeToFile:studentPath atomically:YES]) {
NSLog(@"存储笨蛋完毕>>.....<<");
}
//=========================================================
//解归档
//1. 从磁盘读取文件,生成NSData实例
NSData *unarchiverData = [NSData dataWithContentsOfFile:studentPath];
//2. 根据Data实例创建和初始化解归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:unarchiverData];
//3. 解归档,根据key值访问
Student *UnarchiverStudent1 = [unarchiver decodeObjectForKey:@"studentOne"];
Student *UnarchiverStudent2 = [unarchiver decodeObjectForKey:@"studentTwo"];
NSLog(@" %@ \n %@ ",UnarchiverStudent1
,UnarchiverStudent2);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self getDocumentAppendingPathByFileName:@"class.plist"];
}
- (NSString*)getDocumentAppendingPathByFileName:(NSString*)fileName {
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
//追加文件名称
NSString *filePath = [documentPath stringByAppendingPathComponent:fileName];
NSLog(@"filePath===========%@",filePath);
return filePath;
}
#pragma mark //解归档
- (void)texstOne {
NSString *fileOne = [self getDocumentAppendingPathByFileName:@"fileOne.archiver"];
//归档(序列化)
NSArray *aryOne = @[@"jereh",@"ios"];
if ([NSKeyedArchiver archiveRootObject:aryOne toFile:fileOne]) {
NSLog(@"归档序列化成功........");
}
//解归档(反序列化)
NSArray *unarChiverArrayOne = [NSKeyedUnarchiver unarchiveObjectWithFile:fileOne];
NSLog(@"unarChiverArrayOne=======%@",unarChiverArrayOne);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end