#import// 創(chuàng)建文件或者文檔所在的路徑
typedef NS_ENUM(NSInteger, FilePathType) {
DocmuntsType,
CachesType
};
@interface EncodeManager : NSObject
/*
一几迄、沙盒文件管理操作功能
1.Model歸檔 傳入Model 以及 key值
2.反歸檔Model 傳入文檔名稱 以及 key值
3.數(shù)組歸檔 傳入數(shù)組 以及key值
4.反歸檔數(shù)組 傳入文檔名稱 以及key值
二、創(chuàng)建/獲取 文件夾 和 文檔的功能
1.傳入文件夾名稱在Documents/Caches創(chuàng)建或者獲取文件夾整體路徑的的功能
2傳入文檔名稱在Documents/Caches創(chuàng)建或者獲取文檔整體路徑的功能
*/
+ (EncodeManager *)shareInstance;
/**********
以下是歸檔反歸檔的操作方法
**********/
#pragma mark --- 復(fù)雜對(duì)象(單獨(dú)Model)歸檔 ---
// 傳入model 進(jìn)行歸檔 返回model的NSData
- (NSData *)archiverModel:(id)model modelKey:(NSString *)modelKey;
#pragma mark -- 復(fù)雜對(duì)象(單獨(dú)Model)反歸檔 ---
// 傳入歸檔model寫(xiě)入文件的路徑 返回該model 后面?zhèn)魅雖odel對(duì)象 要通過(guò)model對(duì)象 得到model歸檔時(shí)的key值
- (id)unArchiverModelWithFilePath:(NSString *)filePath modelKey:(NSString *)modelKey;
#pragma mark --- 復(fù)雜對(duì)象(數(shù)組裝載model)歸檔
- (NSData *)archiverArray:(NSArray *)array arrayKey:(NSString *)arrayKey;
#pragma mark --- 復(fù)雜對(duì)象 (數(shù)組裝載model)反歸檔
- (NSArray *)unArchiverArrayWithFilePath:(NSString *)filePath arrayKey:(NSString *)arrayKey;
/**********
以下是在沙盒中Documents/Caches路徑下操作文件或文件夾管理的方法
**********/
#pragma mark --- 創(chuàng)建/獲取文件夾的方法 ---
// 傳入文件夾的名稱 返回整個(gè)路徑 傳入0 在documents文件下創(chuàng)建文件夾 傳入1 在caches文件下創(chuàng)建文件夾
- (NSString *)creatOrGetFileWithFileName:(NSString *)fileName type:(FilePathType)type;
#pragma mark --- 創(chuàng)建/獲取文檔的方法 ---
// 傳入文檔的名稱 返回整個(gè)路徑? 傳入0 在documents文件下創(chuàng)建文檔 傳入1 在caches文件下創(chuàng)建文件
- (NSString *)creatOrGetDocWithWithDocName:(NSString *)docName type:(FilePathType)type;
#pragma mark --- 獲取Documents路徑 ---
- (NSString *)documentsFilePath;
#pragma mark --- 獲取Library/Caches路徑 ---
- (NSString *)cachesFilePath;
@end
#import "EncodeManager.h"
@implementation EncodeManager
+ (EncodeManager *)shareInstance
{
static EncodeManager *manager = nil;
if (manager == nil)
{
manager = [[EncodeManager alloc] init];
}
return manager;
}
/**********
以下是歸檔反歸檔的操作方法
**********/
#pragma mark --- 復(fù)雜對(duì)象(單一Model數(shù)據(jù)模型)的歸檔 ---
- (NSData *)archiverModel:(id)model modelKey:(NSString *)modelKey
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:model forKey:modelKey];
[archiver finishEncoding];
return data;
}
#pragma mark -- 復(fù)雜對(duì)象(單獨(dú)Model)反歸檔 ---
// 傳入歸檔model寫(xiě)入文件的路徑 返回該model? 要通過(guò)model對(duì)象 得到model歸檔時(shí)的key值
- (id)unArchiverModelWithFilePath:(NSString *)filePath modelKey:(NSString *)modelKey
{
// 2.從路徑中獲取數(shù)據(jù)
NSData *data = [NSData dataWithContentsOfFile:filePath];
// 3.創(chuàng)建反歸檔對(duì)象
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
// 4.進(jìn)行對(duì)model的反歸檔讀取
id model = [unArchiver decodeObjectForKey:modelKey];
return model;
}
#pragma mark --- 復(fù)雜對(duì)象(數(shù)組裝載model)歸檔
- (NSData *)archiverArray:(NSArray *)array arrayKey:(NSString *)arrayKey
{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[archiver encodeObject:array forKey:arrayKey];
[archiver finishEncoding];
return data;
}
#pragma mark --- 復(fù)雜對(duì)象 (數(shù)組裝載model)反歸檔
- (NSArray *)unArchiverArrayWithFilePath:(NSString *)filePath arrayKey:(NSString *)arrayKey
{
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
NSArray *array = [unArchiver decodeObjectForKey:arrayKey];
return array;
}
/**********
以下是在沙盒中Documents/Caches路徑下操作文件或文件夾管理的方法
**********/
#pragma mark --- 創(chuàng)建/獲取文件夾的方法 ---
// 傳入文件夾的名稱 返回整個(gè)路徑 傳入0 在documents文件下創(chuàng)建文件夾 傳入1 在caches文件下創(chuàng)建文件夾
- (NSString *)creatOrGetFileWithFileName:(NSString *)fileName type:(FilePathType)type
{
NSString *filePath;
if (type == DocmuntsType)
{
filePath = [[self documentsFilePath] stringByAppendingPathComponent:fileName];
}
else
{
filePath = [[self cachesFilePath] stringByAppendingPathComponent:fileName];
}
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:filePath])
{
[manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
return filePath;
}
#pragma mark --- 創(chuàng)建/獲取文檔的方法 ---
// 傳入文檔的名稱 返回整個(gè)路徑? 傳入0 在documents文件下創(chuàng)建文檔 傳入1 在caches文件下創(chuàng)建文件
- (NSString *)creatOrGetDocWithWithDocName:(NSString *)docName type:(FilePathType)type
{
NSString *filePath;
if (type == DocmuntsType)
{
filePath = [[self documentsFilePath] stringByAppendingPathComponent:docName];
}
else
{
filePath = [[self cachesFilePath] stringByAppendingPathComponent:docName];
}
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:filePath])
{
[manager createFileAtPath:filePath contents:nil attributes:nil];
}
return filePath;
}
#pragma mark --- 獲取Documents路徑 ---
- (NSString *)documentsFilePath
{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
}
#pragma mark --- 獲取Library/Caches路徑 ---
- (NSString *)cachesFilePath
{
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
}
@end