#import <Foundation/Foundation.h>
@interface NSArray (Helper)
+(instancetype)shareInstance;
-(id)CheckError;
/**
* 获取Documents目录路径
*
* @return 返回Documents目录路径
*/
-(NSString *)documentPath;
/**
* 获取当前程序包中一个资源文件路径
*
* @param name 文件名称
* @param type 文件类型
*
* @return 返回资源文件路径
*/
-(NSString *)projectPath:(NSString *)name fileType:(NSString *)type;
/**
* 写入文件到Documents目录
*
* @param fileName 文件名称
*/
-(void)writeToDocument:(NSString *)fileName;
/**
* 写入文件到工程目录
*
* @param fileName 文件名称
* @param type 文件类型
*/
-(void)writeToProject:(NSString *)fileName fileType:(NSString *)type;
/**
* 从Documents目录加载文件数据
*
* @param fileName 文件名称
*
* @return 返回数据到一个数组
*/
-(NSArray *)loadFromDocument:(NSString *)fileName;
/**
* 从工程加载文件数据
*
* @param fileName 文件名称
* @param type 文件类型
*
* @return 返回数据到一个数组
*/
-(NSArray *)loadFromProject:(NSString *)fileName fileType:(NSString *)type;
/**
* 文件归档
*
* @param fileName 文件名称
*/
-(void)archiveToDocument:(NSString *)fileName;
/**
* 文件解归档
*
* @param fileName 文件名称
*
* @return 返回解归档数据
*/
-(NSArray *)unarchiveFromDocument:(NSString *)fileName;
/**
* @brief 防止越界,越界返回Nil
*
* @param index index
*
* @return id
*/
- (id)objectAtIndexCheck:(NSUInteger)index;
@end
#import "NSArray+Helper.h"
@implementation NSArray (Helper)
+(instancetype)shareInstance{
static dispatch_once_t once;
static id instance;
dispatch_once(&once, ^{
instance = [[self alloc]init];
});
return instance;
}
-(id)CheckError
{
if ([self isKindOfClass:[NSNull class]] || !self)
return [[NSArray alloc]init];
else
return self;
}
-(NSString *)documentPath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [paths objectAtIndex:0];
}
-(void)writeToDocument:(NSString *)fileName{
NSString *documentPath = [self documentPath];
NSString *savePath = [documentPath stringByAppendingPathComponent:fileName];
[self writeToFile:savePath atomically:YES];
}
-(NSString *)projectPath:(NSString *)name fileType:(NSString *)type {
NSString *path = [[NSBundle mainBundle]pathForResource:name ofType:type];
return path;
}
-(void)writeToProject:(NSString *)fileName fileType:(NSString *)type {
NSString *savePath = [self projectPath:fileName fileType:type];
[self writeToFile:savePath atomically:YES];
}