iOS开发之沙盒介绍和使用

 //沙盒是一个文件夹,每一个应用程序安装完成的时候,系统会自动生成一个沙盒文件夹,来存放当前应用程序的所需要的持久数据。

//   苹果对沙盒做了一个安全操作。当前应用程序不能访问当前沙盒以外的其他文件夹。

//  iOS8.0之后的沙盒路径下,包含了三部分内容,iOS8.0之前的还包含了.app文件,也就是我们所谓的程序包

   /**

    * 1、Documents:存放文本数据信息的地方。

   2.library/caches:存放缓存文件,数据量比较大得多媒体文件,比如图片、音频、视频。

    3.library/preference:NSUserDefaults存储的用户偏好设置

    4.tmp:暂存区,半成品,比如未下载好的视频。音频下载好了之后,系统会自动剪切操作,将tmp里面的对象放到Caches里面去。

//   获取沙盒路径

     NSString * homePath=  NSHomeDirectory();

   NSLog(@"%@",homePath);

   

//   获取documents文件路径

//   1.

   NSString* docPath = [homePath stringByAppendingString:@"/documents"];

   NSLog(@"%@",docPath);

   

//   2.

   NSString* docPath2 = [homePath stringByAppendingPathComponent:@"documents"];

   NSLog(@"%@",docPath2);

//   3.

   NSString* docPath3=  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) lastObject];

 

   NSLog(@"%@",docPath3);


-(void)write

{

// 获取documents

   NSString* docPath =  [self docPath];

   

   

//   创建一个.txt文件在document路径下

   NSString* filePath = [docPath stringByAppendingPathComponent:@"a.txt"];

//   将字符串写入到本地的a.txt文件中

  

//   NSString * str = @"hello";

   

   

   NSArray *  arr= @[@"www", @"aaa"];

   

//  如a.txt文件不存在,此时会创建a.txt文件,如果写入的时候已经存在a.txt文件,会覆盖之前的a.txt文件。

//   [  str writeToFile:filePathatomically:YES encoding:NSUTF8StringEncoding error:nil];

   [arr writeToFile:filePathatomically:YES];

 

}


-(void)read

{

//   先获取到Document路径

   NSString* docPath = [selfdocPath];

// 获取a.txt文件的路径

   NSString* filePath =  [docPathstringByAppendingPathComponent:@"a.txt"];

//   NSString * str =[NSStringstringWithContentsOfFile:filePath encoding:NSUTF8StringEncodingerror:nil];

   NSArray* array = [NSArrayarrayWithContentsOfFile:filePath];

   ((UILabel*) self.view.subviews[3]).text =array[1];

   }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值