zoukankan      html  css  js  c++  java
  • iOS开发笔记--iOS沙盒机制

    1、iOS沙盒机制

         1、1、目录结构

              默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp。因为应用的沙盒机制,应用只能在几个目录下读写文件
              Documents:苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备份和恢复的时候会包括此目录
              Library:存储程序的默认设置或其它状态信息;

              Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除

              tmp:提供一个即时创建临时文件的地方。

          1、2、获取程序的Home、Tmp目录

              NSString *homeDirectory = NSHomeDirectory(); 

              NSString *tmpDir = NSTemporaryDirectory();

          1、3、获取document、Library、Caches目录

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

              NSString *path = [paths objectAtIndex:0];

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

              NSString *path = [paths objectAtIndex:0];

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

              NSString *path = [paths objectAtIndex:0]; 

    2、写入文件

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

           NSString *docDir = [paths objectAtIndex:0];

           if (!docDir) {  

            NSLog(@"Documents 目录未找到");          

           }  

           NSArray *array = [[NSArray alloc] initWithObjects:@"内容",@"content",nil];  

           NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  

           [array writeToFile:filePath atomically:YES];

     读取文件

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

           NSString *docDir = [paths objectAtIndex:0];  

           NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];  

           NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];  

           NSLog(@"%@", array);

  • 相关阅读:
    python json.dumps() json.dump()的区别
    geopy 在python中的使用
    socket技术详解(看清socket编程)
    数据结构之各种数据结构插入、删除、查找的时间复杂度
    数组查找的时间复杂度正确表述
    各种排序算法时间复杂度
    MySQL将一张表的某些列数据,复制到另外一张表,并且修改某些内容
    Java虚拟机学习
    Java虚拟机学习
    java集合框架05——ArrayList和LinkedList的区别
  • 原文地址:https://www.cnblogs.com/ios4kerwin/p/5032237.html
Copyright © 2011-2022 走看看