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);

  • 相关阅读:
    kubectl 命令详解
    codeforce344 C report
    poj3041 建图+最小顶点覆盖(最大匹配数)
    poj1637 混合欧拉回路的判定
    poj1149 最大流好题 难在建图 好题
    targan 算法模板
    poj2186 强连通分量 targan算法的应用
    poj2723 2分 + 2-sat
    poj3061 尺取法
    poj3207 2-sat基础题
  • 原文地址:https://www.cnblogs.com/ios4kerwin/p/5032237.html
Copyright © 2011-2022 走看看