复杂对象持久化就是将复杂对象写入文件.
复杂对象就是Foundation 内不存在的数据类 ,他们是无法直接通过writeToFile:方法写入文件的, 只能通过将复杂对象转换为NSData,然后再写入文件,而这里将复杂对象转换成NSData类型的过程就是归档.
同样我们从文件中读取复杂对象是需要先读取NSData类型数据, 再将其转化为复杂对象,这个过程就是反归档
具体的过程如下代码
****************************************************************************************
创建一个复杂对象Person类
① 遵循协议
//
// Person.h
// 归档和反归档
//
// Created by lanou3g on 16/2/24.
// Copyright © 2016 年 GlongF. All rights reserved.
//
#import <Foundation/Foundation.h>
// Person.h
// 归档和反归档
//
// Created by lanou3g on 16/2/24.
// Copyright © 2016 年 GlongF. All rights reserved.
//
#import <Foundation/Foundation.h>
//
复杂对象想使用归档的前提是遵守
NSCoding
协议
@interface Person : NSObject < NSCoding >
@property ( nonatomic , strong ) NSString *name;
@property ( nonatomic , assign ) NSInteger age;
@end
**********************************************************************
② 实现协议中两个的方法
//
// Person.m
// 归档和反归档
//
// Created by lanou3g on 16/2/24.
// Copyright © 2016 年 GlongF. All rights reserved.
//
#import "Person.h"
@implementation Person
// 实现协议中的方法
// 编码
- ( void )encodeWithCoder:( NSCoder *)aCoder{
[aCoder encodeObject : self . name forKey : @"name" ];
[aCoder encodeInteger : self . age forKey : @"age" ];
}
// 反编码
- ( instancetype )initWithCoder:( NSCoder *)aDecoder{
self = [ super init ];
if ( self ) {
self . name = [aDecoder decodeObjectForKey : @"name" ];
self . age = [aDecoder decodeIntegerForKey : @"age" ];
}
return self ;
}
// Person.m
// 归档和反归档
//
// Created by lanou3g on 16/2/24.
// Copyright © 2016 年 GlongF. All rights reserved.
//
#import "Person.h"
@implementation Person
// 实现协议中的方法
// 编码
- ( void )encodeWithCoder:( NSCoder *)aCoder{
[aCoder encodeObject : self . name forKey : @"name" ];
[aCoder encodeInteger : self . age forKey : @"age" ];
}
// 反编码
- ( instancetype )initWithCoder:( NSCoder *)aDecoder{
self = [ super init ];
if ( self ) {
self . name = [aDecoder decodeObjectForKey : @"name" ];
self . age = [aDecoder decodeIntegerForKey : @"age" ];
}
return self ;
}
**********************************************************************
③ 持久化过程
//
// ViewController.m
// 归档和反归档
//
// Created by lanou3g on 16/2/24.
// Copyright © 2016 年 GlongF. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- ( void )viewDidLoad {
// ViewController.m
// 归档和反归档
//
// Created by lanou3g on 16/2/24.
// Copyright © 2016 年 GlongF. All rights reserved.
//
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- ( void )viewDidLoad {
[super
viewDidLoad
];
//
获取
document
路径
NSString
*documentPath = [
NSSearchPathForDirectoriesInDomains
(
NSDocumentDirectory
,
NSUserDomainMask
,
YES
)
lastObject
];
NSLog ( @"doc = %@" ,documentPath);
#pragma mark ------------------------ 复杂对象持久化 -----------------
// 过程 : 复杂对象 -> 归档 -> NSData -> 写入文件
Person *person = [[ Person alloc ] init ];
person. age = 22 ;
person. name = @" 张三 " ;
NSMutableData *data = [ NSMutableData data ];
// 创建归档器
NSKeyedArchiver *archiver = [[ NSKeyedArchiver alloc ] initForWritingWithMutableData :data];
// 归档
NSLog ( @"doc = %@" ,documentPath);
#pragma mark ------------------------ 复杂对象持久化 -----------------
// 过程 : 复杂对象 -> 归档 -> NSData -> 写入文件
Person *person = [[ Person alloc ] init ];
person. age = 22 ;
person. name = @" 张三 " ;
NSMutableData *data = [ NSMutableData data ];
// 创建归档器
NSKeyedArchiver *archiver = [[ NSKeyedArchiver alloc ] initForWritingWithMutableData :data];
// 归档
[archiver
encodeObject
:person
forKey
:
@"person"];
//
结束归档
[archiver
finishEncoding
];
// 将归档结束的 data 写入文件
NSString *filePath = [documentPath stringByAppendingPathComponent : @"personFile.txt" ];
[data writeToFile :filePath atomically : YES ];
NSLog ( @"filePath = %@" ,filePath);
NSLog ( @"data = %@" ,data);
#pragma mark -- 从文件中读取复杂对象
// 过程 ( 读取文件 -> NSData -> 反归档 -> 复杂对象 )
// 读取数据
NSData *dataRead = [ NSData dataWithContentsOfFile :filePath];
// 创建反归档工具
NSKeyedUnarchiver *unArchiver = [[ NSKeyedUnarchiver alloc ] initForReadingWithData :dataRead];
Person *person1 = [unArchiver decodeObjectForKey : @"person1" ];
[unArchiver finishDecoding ];
NSLog ( @"person name = %@" ,person1. name );
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
// 将归档结束的 data 写入文件
NSString *filePath = [documentPath stringByAppendingPathComponent : @"personFile.txt" ];
[data writeToFile :filePath atomically : YES ];
NSLog ( @"filePath = %@" ,filePath);
NSLog ( @"data = %@" ,data);
#pragma mark -- 从文件中读取复杂对象
// 过程 ( 读取文件 -> NSData -> 反归档 -> 复杂对象 )
// 读取数据
NSData *dataRead = [ NSData dataWithContentsOfFile :filePath];
// 创建反归档工具
NSKeyedUnarchiver *unArchiver = [[ NSKeyedUnarchiver alloc ] initForReadingWithData :dataRead];
Person *person1 = [unArchiver decodeObjectForKey : @"person1" ];
[unArchiver finishDecoding ];
NSLog ( @"person name = %@" ,person1. name );
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
@end