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

  • 相关阅读:
    分类与监督学习,朴素贝叶斯分类算法
    K-means算法应用:图片压缩
    聚类--K均值算法:自主实现与sklearn.cluster.KMeans调用
    numpy统计分布显示
    10.11作业numpy数据集练习
    9.29作业
    CAGradientlayer设置视图背景的渐变效果
    dyld: Library not loaded: @rpath/libswiftCore.dylib
    解读NSString之性能分析
    iOS UIButton超出父视图无法点击解决方法
  • 原文地址:https://www.cnblogs.com/ios4kerwin/p/5032237.html
Copyright © 2011-2022 走看看