iOS沙盒文件存储

本文介绍了如何在iOS应用中使用沙盒进行文件存储,包括将JSON数据写入文件、读取JSON数据、转换字符串为JSON格式、删除JSON文件以及遍历沙盒中的所有文件。详细讲解了各个函数的实现方法,如writeToFile、getDataWithFileName等,以帮助开发者更好地管理和操作应用数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

在h文件中:

//json数据暂存在沙盒

+(void)writeToFile:(NSDictionary*)dic WithName:(NSString*)fileName;


//读取预存的json数据

+(NSDictionary*)getDataWithFileName:(NSString*)fileName;


//将字符串转为json格式

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString;


//删除json文件

+(void)removeJsonFile:(NSString*)fileName;


//遍历沙盒下所有文件

+(void)seekAllFile;




m文件:


//json数据暂存在文件里

+(void)writeToFile:(NSDictionary*)dic WithName:(NSString*)fileName{

    

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *path=[pathsobjectAtIndex:0];

    NSString *Json_path=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.json",fileName]];

    NSError *err;

    NSData * data=[NSJSONSerializationdataWithJSONObject:dic options:NSJSONWritingPrettyPrintederror:&err];

    if (err) {

        NSLog(@"%@",err);

    }

    else{

        //==写入文件

        NSLog(@"%@",[datawriteToFile:Json_path atomically:YES] ?@"Succeed":@"Failed");

    }

}


//读取预存的json数据

+(NSDictionary*)getDataWithFileName:(NSString*)fileName

{

    //读取Json

    //==Json文件路径

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *path=[pathsobjectAtIndex:0];

    NSString *Json_path=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.json",fileName]];

    NSData * data=[[NSDataalloc]initWithContentsOfFile:Json_path];

    if (data) {

    NSError *err;

    NSDictionary * dic=[NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingAllowFragmentserror:&err];

    if(err) {

        NSLog(@"json解析失败:%@",err.userInfo);

    }

    else{

        NSLog(@"解析成功:%@",dic);

        return dic;

    }

    }

    else{

        NSLog(@"该文件无数据或不存在:%@",Json_path);

    }

    returnnil;

}



//将字符串转为json格式

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {

    if (jsonString ==nil) {

        returnnil;

    }

    NSData *jsonData = [jsonStringdataUsingEncoding:NSUTF8StringEncoding];

    NSError *err;

    NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:jsonData

                                                        options:NSJSONReadingMutableContainers

                                                          error:&err];

    if(err) {

        NSLog(@"json解析失败:%@",err);

        returnnil;

    }

    return dic;

}

//删除json文件

+(void)removeJsonFile:(NSString*)fileName

{

    NSFileManager * fileM=[NSFileManagerdefaultManager];

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

    NSString *path=[pathsobjectAtIndex:0];

    NSString *Json_path=[pathstringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.json",fileName]];

    BOOL blHave=[[NSFileManagerdefaultManager] fileExistsAtPath:Json_path];

    if (!blHave) {

        NSLog(@"文件不存在:%@",fileName);

    }

    else{

        NSError * err;

        BOOL blDele= [fileMremoveItemAtPath:Json_patherror:&err];

        if (!blDele) {

            NSLog(@"文件删除失败%@",err.userInfo);

        }

    }

  

}

//遍历沙盒下所有文件

+(void)seekAllFile{

    //得到沙盒文件夹下的所有文件

    NSFileManager *fileManager = [NSFileManagerdefaultManager];

    NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];

    NSArray *fileList;

    fileList =[fileManager contentsOfDirectoryAtPath:documenterror:NULL];

    for (NSString *filein fileList) {

        NSLog(@"%@",file);

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值