首先创建一个归档用的工具类
BaseKeyedArchiver
.h中代码:
//
// BaseKeyedArchiver.h
// DataWriteToFile
//
// Created by limin on 16/7/18.
// Copyright © 2016年 君安信(北京)科技有限公司. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BaseKeyedArchiver : NSObject
/**
* 单例
*
* @return 返回单例对象
*/
+ (instancetype)sharedClient;
/**
* 对数据进行归档
*
* @param dataArray 需要进行归档的数据
*/
-(void)setArchiverDataWithData:(NSMutableArray *)dataArray;
/**
* 获取解归档的数据
*
* @return 解归档后的数组
*/
-(NSMutableArray*)getArchiverData;
/**
* 获取文件路径
*
* @return 返回文件路径
*/
-(NSString *)getAllFilePath;
@end
.m中代码
//
// BaseKeyedArchiver.m
// DataWriteToFile
//
// Created by limin on 16/7/18.
// Copyright © 2016年 君安信(北京)科技有限公司. All rights reserved.
//
#import "BaseKeyedArchiver.h"
/**
* 文件后缀名
*/
NSString *const kFileDefaultPathExtension = @"cache";
/**
* 默认文件名
*/
NSString *kFileDefaultFileName = @"defaultFile";
@interface BaseKeyedArchiver ()
@end
@implementation BaseKeyedArchiver
/**
* 单例
*
* @return 返回单例对象
*/
+ (instancetype)sharedClient
{
static BaseKeyedArchiver *client = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
client = [[BaseKeyedArchiver alloc]init];
});
// kFileDefaultFileName = [[NSUserDefaults standardUserDefaults]valueForKey:isLoginStatus]?[[NSUserDefaults standardUserDefaults]valueForKey:PHONE]:@"defaultFile";
return client;
}
/**
* 对数据进行归档
*
* @param dataArray 需要进行归档的数据
*/
-(void)setArchiverDataWithData:(NSMutableArray *)dataArray
{
NSString *fileAllPath = [self getAllFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:fileAllPath]) {//先将该地址存在的文件删除,再存储。
NSError *error = nil;
if(![[NSFileManager defaultManager] removeItemAtPath:fileAllPath error:&error]) {
NSLog(@"Cannot remove file: %@", error);
}
}
[NSKeyedArchiver archiveRootObject:dataArray toFile:fileAllPath];
}
/**
* 获取解归档的数据
*
* @return 解归档后的数组
*/
-(NSMutableArray*)getArchiverData
{
NSString *fileAllPath = [self getAllFilePath];
NSMutableArray *newDataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:fileAllPath];
return newDataArray;
}
/**
* 获取文件路径
*
* @return 返回文件路径
*/
-(NSString *)getAllFilePath
{
NSString *paths = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *fileAllPath = [[paths stringByAppendingPathComponent:kFileDefaultFileName] stringByAppendingPathExtension:kFileDefaultPathExtension];
return fileAllPath;
}
@end
需要存储的数据,创建一个model对象。并在model.m中加入归档信息
.h代码
//
// LMDraftModel.h
// ImageSelectOrArchive
//
// Created by limin on 16/12/13.
// Copyright © 2016年 君安信(北京)科技有限公司. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface LMDraftModel : NSObject
/* 文章标题 */
@property(nonatomic,copy)NSString *articleTitle;
/* 时间 */
@property(nonatomic,copy)NSString *time;
/* 图片数组 */
@property(nonatomic,strong)NSArray *pictureArrays;
/** 图片资源*/
@property(nonatomic,strong)NSArray *assetArrays;
/* 图片来源 */
@property(nonatomic,assign)BOOL OriginalPhoto;
@end
.m代码
//
// LMDraftModel.m
// ImageSelectOrArchive
//
// Created by limin on 16/12/13.
// Copyright © 2016年 君安信(北京)科技有限公司. All rights reserved.
//
#import "LMDraftModel.h"
#import <objc/runtime.h>
@interface LMDraftModel()<NSCoding>
@end
@implementation LMDraftModel
#pragma mark - 归档
-(void)encodeWithCoder:(NSCoder *)aCoder
{
unsigned int outCount = 0;
Ivar *ivars = class_copyIvarList([self class], &outCount);
for (unsigned int i = 0; i<outCount; i++) {
Ivar ivar = ivars[i];
NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
id value = [self valueForKey:key];
[aCoder encodeObject:value forKey:key];
}
free(ivars);
}
#pragma mark - 解归档
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
unsigned int OutCount = 0;
Ivar *ivars = class_copyIvarList([self class], &OutCount);
for (unsigned int i=0; i<OutCount; i++) {
Ivar ivar = ivars[i];
NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];
[self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
}
free(ivars);
}
return self;
}
@end
数据归档方法:
#pragma mark - 保存到草稿箱
-(void)saveDraftBox
{
//时间
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *destDateString = [dateFormatter stringFromDate:[NSDate date]];
//model
LMDraftModel *model = [[LMDraftModel alloc]init];
model.time = destDateString;
model.articleTitle = self.titleTextField.text;
model.pictureArrays = self.selectedPhotos;
//归档数据{A:先取出存储的数据,B:判断数据是否已存在,(1,第一次进去,直接插入数据。2:从草稿箱进去的,直接替换对应的数据)}
NSMutableArray *draftArrays = [[BaseKeyedArchiver sharedClient]getArchiverData];
if (!draftArrays) {
draftArrays = [NSMutableArray array];
}
if (self.isFromDraftBox) {
[draftArrays replaceObjectAtIndex:self.draftIndex withObject:model];
}else
{
[draftArrays insertObject:model atIndex:0];
}
[[BaseKeyedArchiver sharedClient]setArchiverDataWithData:draftArrays];
}
获取归档的数据:
-(void)getUnArchiveData
{
//解析文件数据
NSArray *array = [[BaseKeyedArchiver sharedClient]getArchiverData];
}
版权声明:本文为Leemin_ios原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。