沙盒机制

iOS沙盒机制
iOS应用程序只能在为该改程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等。
1、每个应用程序都有自己的存储空间
2、应用程序不能翻过自己的围墙去访问别的存储空间的内容
3、应用程序请求的数据都要通过权限检测,假如不符合条件的话,不会被放行。

沙盒主路径

程序运行期间,系统会生成一个专属的沙盒路径,应用程序在使用期间的非代码文件都存储在当前的沙盒路径里面

NSString *homePath = NSHomeDirectory();
    NSLog(@"%@",homePath);

通过Xcode打印出来的路径,粘贴到finder中的前往文件夹就查看到了目录

目录结构

默认情况下,每个沙盒有三个文件夹:Documents,Library和tmp. 因为应用的沙盒机制,应用智能在几个目录下读写文件
Documents:用来存储永久性的数据的文件,程序运行时必要的文件都存储在这里(数据库),itunses会自动备份这里的文件
Library:用于保存程序运行期间生成的文件;
Library/Caches:用于保存程序运行期间 产生的缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
tmp:提供一个即时创建临时文件的地方,程序运行期间产生的临时碎片会保存到这个文件夹里,通常文件下载完之后或者程序退出会自动清空此文件夹,ituns不会备份这里的数据。

简单对象写入文件

NSString写入文件

//1.准备字符串
    NSString *string = @"we are family";
    
    //2.准备路径
   NSString *path = NSHomeDirectory();
    path = [path stringByAppendingString:@"咒语.txt"];
    
    //3.写入文件
    //第一个参数:路径
    //第二个参数:是否进行线性操作(YES的时候,保证发生意外时有中转文件来保存信息,直至写入完成,但是损耗大; NO的时候,写入速度快,但是没有安全保证)
    //第三个参数:编码方式
    //第四个参数:错误对象
   
    [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    //4.打印路径
    NSLog(@"%@",path);
    
//    //读取文件
//    //第一个参数:路径
//    //第二个参数:编码方式(切记要和写入时用相同的编码方式)
//    //第三个参数:错误信息
    
    NSString *contentString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@",contentString);

NSArray写入文件

    NSArray *array = [NSArray arrayWithObjects:@"Lily",@"Yucui",@"Star",@"Ling",@"WenQI",@"Yangyang", nil];
    
    NSString *docuPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    docuPath = [docuPath stringByAppendingString:@"/Lady.txt"];
    
    //写入文件
    [array writeToFile:docuPath atomically:YES];
    
    NSLog(@"%@",docuPath);

    //读取文件
    NSArray *array1 = [NSArray arrayWithContentsOfFile:docuPath];
    NSLog(@"%@",array1);

NSDicitionary写入文件

   NSString *path1 = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
    
  NSString *path2 = [path1 stringByAppendingString:@"/PreferencePans"];
    
     NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"HK",@"hkhk",@"jiji",@"ji",@"kiki",@"ki", nil];
     NSLog(@"%@",path2);
    //写入
    [dic writeToFile:path2 atomically:YES];
    //读取
    NSDictionary *dic1 = [NSDictionary dictionaryWithContentsOfFile:path2];
    NSLog(@"%@",dic1);
    ```
***NSData写入文件***

//1.获取图片对象
UIImage *image = [UIImage imageNamed:@"999.png"];

//2.准备路径
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"999.png"];

//3.将图片对象转换成Data
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:homePath atomically:YES];
NSLog(@"%@",homePath);

//4.
UIImageView *imgView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imgView.image = [UIImage imageWithContentsOfFile:homePath];
[self.view addSubview:imgView];
#复杂对象写入文件
简单对象可以通过writeToFile写入的方式,但是复杂对象没有writeToFile的方法写入文档,所以我们需要借助归档和反归档的方法,将复杂对象转换成简单对象,写入文档

SingleVip *vip = [SingleVip new];
vip.name = @"法特";
vip.assets = @"数不清";
vip.car = @"兰博基尼";
vip.age = 18;

//准备路径
NSString *homePath = NSHomeDirectory();
homePath = [homePath stringByAppendingPathComponent:@"钻石王老五.txt"];


//创建数据对象,用来存放vip对象
NSMutableData *data = [NSMutableData data];
//创建归档对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
//开始归档
[archiver encodeObject:vip forKey:@"vip"];
//完成归档
[archiver finishEncoding];
//写入文件
[data writeToFile:homePath atomically:YES];
 NSLog(@"%@",homePath);


//反归档
//将文件里的data对象读取出来
NSData *_data  = [NSData dataWithContentsOfFile:homePath];
//创建反归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:_data];
SingleVip *_vip = [unarchiver decodeObjectForKey:@"vip"];
//完成反归档
[unarchiver finishDecoding];

NSLog(@"%@",_vip.name);
注意:    归档并不是数据持久化,而是辅助复杂对象转化成简单对象的一种方式,真正实现数据持久化的仍然是writeTOFile 写入文件
    数据持久化的方式有:
    plist(属性列表)
    偏好设置  NSUserDefaults(单列)
    writeToFile 写入文件
    SQLite  数据库
    CoreData





最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容