OC 数据归档
2017-04-19 本文已影响114人
Maj_sunshine
数据归档作为一种轻量级的数据存储方式,和属性列表不同之处在于存储的对象广泛,几乎任何类型的对象都能被归档存储且存储是进行加密处理的,归档时会转换和二进制数据,安全性远远高于属性列表。而属性列表只能存储oc基本数据类型和NSArray NSDictionary NSData NSDate NSString 类型。
使用archiveRootObject 对一个对象进行简单的归档。
- (void)archiverOne
{
// 获取归档文件的路径
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"my.archiver"];
NSLog(@"filePath=%@",filePath);
// 一个对象的归档方法 返回值为bool类型
// BOOL isSuccess = [NSKeyedArchiver archiveRootObject:<#(nonnull id)#> toFile:<#(nonnull NSString *)#>]
BOOL isSuccess = [NSKeyedArchiver archiveRootObject:@"一个对象归档" toFile:filePath];
if (isSuccess) {
NSLog(@"归档成功");
}
}
使用NSKeyedUnarchiver的unarchiveObjectWithFile方法解档
- (void)unArchiverOne
{
//获取解档文件路径
NSString *filepath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"my.archiver"];
//使用 NSKeyedUnarchiver 进行解档
[NSKeyedUnarchiver unarchiveObjectWithFile:filepath1];
}
往往我们不只是要保存一个对象,这时候就要多个对象进行归档解档
使用NSKeyedArchiver alloc方法中的initForWritingWithMutableData对多个对象进行归档
- (void)archiverMore // 对多个对象进行归档
{
//获取归档路径
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"demo.archiver"];
//初始化一个可变二进制对象 NSMutableData
NSMutableData *data = [NSMutableData data];
//初始化归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//对存储的数据进行编码
[archiver encodeObject:@"summer" forKey:@"season"];
[archiver encodeInteger:1 forKey:@"count"];
[archiver encodeObject:[NSArray arrayWithObjects:@"张三",@"李四",@"王五", nil] forKey:@"name"];
//编码结束 将数据写入二进制对象
[archiver finishEncoding];
//将二进制对象存储到文件中
BOOL isSuccess = [data writeToFile:filePath atomically:YES];
if (isSuccess) {
NSLog(@"归档成功 ,path= %@",filePath);
}
}
使用NSKeyedArchiver alloc方法中的initForReadingWithData对多个对象进行解档
- (void)unArchiverMore // 对多个对象进行解档
{
//获取解档档路径
NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"demo.archiver"];
//初始化文件中可变二进制对象
NSMutableData *data = [NSMutableData dataWithContentsOfFile:filepath];
//初始化解档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
//接收对应数据
NSString *season = [unarchiver decodeObjectForKey:@"season"];
NSInteger count = [unarchiver decodeIntegerForKey:@"count"];
NSArray *name = [unarchiver decodeObjectForKey:@"name"];
NSLog(@"secson = %@,count = %li,name = %@",season,count,name);
}
再有时候,系统的对象不能满足要求,当对象是服务器端传来的json数据解析而来的model时,以上两种都不能保存,这时候就要求自定义对象遵循NSCoding协议,我们实现协议后进行归档解档
model.h
#import <Foundation/Foundation.h>
@interface model : NSObject <NSCoding> // 保存自定义对象需遵循NSCoding协议并实现
@property(nonatomic,copy)NSString *season ;
@property(nonatomic,copy)NSArray *name;
@property(nonatomic,assign)NSInteger count;
@end
model.m
#import "model.h"
@implementation model
- (void)encodeWithCoder:(NSCoder *)aCoder //归档保存到文件中的时候调用方法,以下归档三个对象
{
[aCoder encodeInteger:self.count forKey:@"count"];
[aCoder encodeObject:self.season forKey:@"season"];
[aCoder encodeObject:self.name forKey:@"name"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder //解档从文件中读取的时候调用方法
{
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.season =[aDecoder decodeObjectForKey:@"season"];
self.count = [aDecoder decodeIntegerForKey:@"count"];
}
return self;
}
@end
遵循协议后用之前的方法进行归档解档
- (void)customArchiver //自定义对象归档
{
model *myModel = [[model alloc]init];
myModel.name = @[@"1",@"2"];
myModel.count = 10;
myModel.season = @"summer";
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"custom.archiver"];
NSLog(@"filePath=%@",filePath);
BOOL isSuccess = [NSKeyedArchiver archiveRootObject:@"一个对象归档" toFile:filePath];
if (isSuccess) {
NSLog(@"归档成功");
}
}
- (void)customUnArchiver
{
NSString *filepath1 = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject]stringByAppendingPathComponent:@"custom.archiver"];
//使用 NSKeyedUnarchiver 进行解档
model *myModel= [NSKeyedUnarchiver unarchiveObjectWithFile:filepath1];
if (myModel) {
NSLog(@"%@,%li,%@", myModel.name, myModel.count, myModel.season);
}
}
菜鸟学习第一天