0
点赞
收藏
分享

微信扫一扫

iOS获取沙盒路径并写入文件

.h文件中
//你需要的数据集合形式
@property(nonatomic,strong)NSMutableArray *groupArray;//数组
@property(nonatomic,strong)NSMutableDictionary *allDict;//字典

//判断沙盒里面是否有需要的对象
+(ShareDataHandleModel*)shareDataHandleModel;


//获取沙盒文件中document路径
+(NSString*)documentPath;


//通过文件名从沙盒读数据
-(void)readDataFromDocumentWithFileName:(NSString*)fileName;//数组

-(void)readDataFromDocumentWithFileNameToDict:(NSString *)fileName;//字典


//写入
-(void)myWirteTofile:(NSString*)fileName;

.m文件中
+(ShareDataHandleModel*)shareDataHandleModel
{
//如果为空 创建一个新的
if (nil==shareModel) {
shareModel=[[ShareDataHandleModel alloc]init];
}
return shareModel;
}


//获取沙盒文件中document路径
+(NSString*)documentPath
{
NSArray *pathArray=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return pathArray[0];
}


//通过文件名从沙盒读数据(数组)
-(void)readDataFromDocumentWithFileName:(NSString*)fileName
{
//拼接文件路径
NSString *filePath=[[[self class]documentPath]stringByAppendingFormat:@"/%@",fileName];

NSArray *tempArray=[NSArray arrayWithContentsOfFile:filePath];

self.groupArray=[NSMutableArray arrayWithObject:tempArray];


}

//通过文件名从沙盒读数据(字典)
-(void)readDataFromDocumentWithFileNameToDict:(NSString *)fileName
{
//拼接文件路径
// NSString *filePath=[[[self class]documentPath]stringByAppendingFormat:@"/%@",fileName];

NSString *filePath=[self appendPath:fileName];//使用下面封装好的
self.allDict=[NSMutableDictionary dictionaryWithContentsOfFile:filePath];
}




//封装
-(NSString*)appendPath:(NSString*)fileName
{

//拼接文件路径
return [[[self class]documentPath]stringByAppendingFormat:@"/%@",fileName];

}


//写入
-(void)myWirteTofile:(NSString*)fileName
{
NSString *filePath=[self appendPath:fileName];

NSMutableArray *a=self.groupArray[0];
[a writeToFile:filePath atomically:YES];


}

举报

相关推荐

0 条评论