iOS 沙盒的使用 写入 读出,删除

1 篇文章 0 订阅
1 篇文章 0 订阅

- (void)viewDidLoad {

    [super viewDidLoad];

    

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

    NSString *documentPath = documentsArr[0];

    NSString *path = [documentPath stringByAppendingPathComponent:@"person.text"];

    NSLog(@"path = %@", path);

    


    // 写入字典

    NSString *dicPath = [documentPath stringByAppendingPathComponent:@"zidian.plist"];

    NSDictionary *dic = @{@"name":@"1",@"age":@"2"};

    [dic writeToFile:dicPath atomically:YES];

    // 写入字符串

    NSString *strPath = [documentPath stringByAppendingPathComponent:@"str.plist"];

    NSString *str = @"这是字符串";

    [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    

    

    [self delete];

    [self read];

    

}


- (void)read{

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

    NSString *documentsPath = [docPath objectAtIndex:0];

    // 写入数组

    NSString *readPath = [documentsPath stringByAppendingPathComponent:@"person.text"];

    NSArray *array = @[@"1", @"2", @"3"];

    [array writeToFile:readPath atomically:YES];

    // 读取数组

    NSArray *array1 = [NSArray arrayWithContentsOfFile:readPath];

    NSLog(@"%@", array1);

   

    // 读出字典

    NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];

    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];

    NSLog(@"dic = %@", dic);

    

    // 读出字符串

    NSString *strPath = [documentsPath stringByAppendingPathComponent:@"str.plist"];

    NSString *str1 = [[NSString alloc] initWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];

    NSLog(@"%@", str1);

}


- (void)delete

{

//    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

//    NSString *documentsPath = [docPath objectAtIndex:0];

//    NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"zidian.plist"];

//    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:dicPath];

//    NSLog(@"%@", dic);

//     NSString *str = dic[@"name"];

    // 删除文件

    NSFileManager *fileManeger = [NSFileManager defaultManager];

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

    NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"zidian.plist"];

//    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];

//    if (!blHave) {

//        return ;

//    }else {

//        BOOL blDele= [fileManeger removeItemAtPath:uniquePath error:nil];

//        if (blDele) {

//            NSLog(@"dele success");

//        }else {

//            NSLog(@"dele fail");

//        }

//        

//    }

   // 结果是布尔类型

     BOOL blDele= [fileManeger removeItemAtPath:uniquePath error:nil];

    // 此时已经文件是空

    NSLog(@"%@", blDele?@"YES":@"NO");

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值