1. 沙盒机制
数据持久化的原因及本质: 存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的.数据持久化是将数据保存成文件,存储到程序的沙盒中.
每个应用程序都有独立的沙盒,就是一个文件夹,名字是随机分配的.每次打开的文件夹路径都不一样.
// 打印沙盒中文件夹的路径
- (void)path
{
   // 每运行一次 相当于从新安装一次  重新安装就从新分配一个沙盒的路径 所以每次运行 路径都不一样
   
    // NSDocumentationDirectory 要打印的文件夹地址
    // NSUserDomainMask 搜索的范围
        /*
        NSUserDomainMask = 1,       // user's home directory 用户目录中
        NSLocalDomainMask = 2,      // local to the current machine --- 当前机器中
        NSNetworkDomainMask = 4,    // publically available location in the local area network --- 网络课件的主机中
        NSSystemDomainMask = 8,     // provided by Apple, unmodifiable (/System)---系统目录 不可修改
        NSAllDomainsMask = 0x0ffff  // all domains: 所有的
        */
    // 返回值是数组
        /*
         lastobject   firstObject   [0]   访问该数组都行
         */
1. document 路径
        /*
         该文件夹 一般存储用户的一些数据
         */
   
   
   
   NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *document = [array firstObject];
    NSLog(@"%@", document);
   
2.缓存文件夹路径
    // 该文件夹 一般存储缓存文件
   
    NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cache = [array1 lastObject];
    NSLog(@"%@", cache);
   
3.tmp文件夹路径
    // 该文件夹 一般存储tmp文件
    NSString *tmp = NSTemporaryDirectory();
    NSLog(@"%@", tmp);
4.打印沙盒的主目录路径
   
    NSString *home = NSHomeDirectory();
    NSLog(@"%@", home);
}
2. 简单对象写入文件
// 简单对象 写入文件
/**
 *  1. 拼接要写入的路径 (要注意的: 路径一定要拼对)
    2. 调用写入方法
 
 
    3. 注意: 如果写入简单对象字典或者数组 那么数组字典中存储的数据 必须是简单对象 无法写入复杂对象
 */
- (void)writeFile
{
    // 简单对象(字符串  字典  数组  data...... 这些系统写好的简单对象)
    // 写入文件的路径 <在documents路径下写入一个叫 /xiaoshuo.txt 的文件>
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [array lastObject];
    // 拼接要写入的文件的路径  // stringByAppendingPathCommponent
    NSString *path = [NSString stringWithFormat:@"%@/xiaoshuo.txt", documentsPath];
    NSLog(@"%@", path);

NSString *string = @"第一章,在一个夜黑风高的早上,我漫步在... ...";
    // 写入的方法
    // atomically 如果yes 在写入的过程中 如果出现程序崩溃 就不应写写入
    [string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
   
   
    // 写入一个数组
    // 必须给后缀 默认是txt 的
    NSString *path1 = [documentsPath stringByAppendingPathComponent:@"array.plist"];
    NSArray *array1 = @[@"1",@"2",@"3",@"4",@"5"];
    [array1 writeToFile:path1 atomically:YES];
   
    // 写入一个字典
    NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"dic.txt"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"A",@"a",@"B",@"b",@"C",@"c",@"D",@"d", nil];
    [dic writeToFile:dicPath atomically:YES];
   
   
    // 写入一个data
    // data 的后缀名是 .da
    NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"data.da"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:dataPath atomically:YES];
   
 
   
    // 复杂对象(自定义的类创建的对象 Person类,Students类)
}
// 读取写入的文件
- (void)readingFile
{
    // 读字符串
    // 获取路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [array lastObject];
    // 拼接要写入的文件的路径  // stringByAppendingPathCommponent
    NSString *path = [NSString stringWithFormat:@"%@/xiaoshuo.txt", documentsPath];
    NSString *string = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"%@", string);
   
    // 读取数组
    NSString *path1 = [documentsPath stringByAppendingPathComponent:@"array.plist"];
    NSArray *array2 = [[NSArray alloc]initWithContentsOfFile:path1];
    NSLog(@"%@", array2);
   
    // 读取字典
    NSString *dicPath = [documentsPath stringByAppendingPathComponent:@"dic.plist"];
    NSDictionary *dic1 = [[NSDictionary alloc]initWithContentsOfFile:dicPath];
    NSLog(@"%@", dic1);
   
   
    // 读取data
    NSString *dataPath = [documentsPath stringByAppendingPathComponent:@"data.da"];
    NSData *data1 =[[NSData alloc]initWithContentsOfFile:dataPath];
    NSString *strings = [[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];

    NSLog(@"%@", strings);
}
3. NSFileMange

// 创建一个文件夹

- (void)creatFile
{
    // 需求 在documents 文件夹先创建一个download文件夹
    //NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = kDocumentsPath;
    // 拼接路径
    NSString *downloadPath = [documentsPath stringByAppendingPathComponent:@"Download"];
    // 创建文件夹
        /*
         文件管理者这个类是个单例类  用来对文件进行操作
         */
    NSFileManager *fileManager = [NSFileManager defaultManager];
       /*
         withIntermediateDirectories   如果是YES情况下 要创建的话 存在的话 可以进行覆盖 反之文件存在的话不能对齐进行覆盖 然后创建失败
         attributes  设置一些属性 (只读.....)
        */
    /**
     *  /Users/lanou3g/Library/Developer/CoreSimulator/Devices/F4C0035C-BFC9-4B9A-95D1-59CE94B63E35/data/Containers/Data/Application/8A4CBE00-1C0D-4E03-893E-B389A42397DE/Documents/Download
     */
   
    BOOL isCreatFile = [fileManager createDirectoryAtPath:downloadPath withIntermediateDirectories:YES attributes:nil error:nil];
    NSLog(@"%d", isCreatFile);
   
   
   
}

// 移动文件夹
/*
 移动需要俩路径
 
 */
- (void)moveFile
{
    // 获取原来的路径
    NSString *oldPath = [kDocumentsPath stringByAppendingPathComponent:@"Download"];
   
    // 获取新路径 library 下的 cache 文件夹
   
    NSString *cachesPath = [kCachesPath stringByAppendingPathComponent:@"Download"];
   
    // 创建 文件管理者的对象
   
    NSFileManager *fileManager1 = [NSFileManager defaultManager];
   
    // 移动文件夹
   
   BOOL isMoveFile = [fileManager1 moveItemAtPath:oldPath toPath:cachesPath error:nil];
    NSLog(@"%d", isMoveFile);
}

// 复制文件夹
- (void)copyFile
{
   
   
    // 获取新路径 library 下的 cache 文件夹 复制到 documents文件夹下
   
    NSString *Path = [kCachesPath stringByAppendingPathComponent:@"Download"];
   
    // 获取需要复制的路径
   
    NSString *Path1 = [kDocumentsPath stringByAppendingPathComponent:@"Download"];
   
    // 创建一个新的文件管理者对象
   
    NSFileManager *fileManger2 = [NSFileManager defaultManager];
   
    // 复制文件夹
   
    BOOL isCopyFile = [fileManger2 copyItemAtPath:Path toPath:Path1 error:nil];
    NSLog(@"%d", isCopyFile);
}

// 删除文件夹

- (void)removeFile
{
    // 获取要删除的文件夹的路径
    NSString *Path1 = [kDocumentsPath stringByAppendingPathComponent:@"Download"];

// 创建一个文件管理者的对象
    NSFileManager *fileManger3 = [NSFileManager defaultManager];

// 删除文件夹
    BOOL isRemoveFile = [fileManger3 removeItemAtPath:Path1 error:nil];
    NSLog(@"%d", isRemoveFile);
   
}

// 判断一个文件夹是否存在 (经常使用)
- (void)isExistFile
{
    // 获取要判断的路径
    NSString *Path = [kCachesPath stringByAppendingPathComponent:@"Download"];
    // 创建文件管理者对象
    NSFileManager *fileManage = [NSFileManager defaultManager];
    // 判断是否存在
    BOOL isExistFile = [fileManage isExecutableFileAtPath:Path];
    NSLog(@"%d", isExistFile);

}
4. 复杂对象的归档
1) 对象类中的归档
.h

/**
 *  复杂对象进行持久化 需要遵守一个协议<NSCoding>
 
    1.
 */

@interface JJModel : NSObject<NSCoding>

@property (nonatomic,retain)NSString *name;
@property (nonatomic,assign)NSInteger age;
@property (nonatomic,retain)NSData *data;

@end
.m
@implementation JJModel
- (void)dealloc
{
    [_data release];
    [_name release];
    [super dealloc];
}

/**
 *  对复杂对象进行持久化 叫做归档与反归档 (即 编码与解码)
 *  1. encodeWithCoder 归档方法
            归档即 编码成可以进行持久化的格式
 *
 */

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    // 对每一个属性都要进行重新编码
    // 注意属性的类型
    // 除了对象类型 其他类型都有 特殊的编码方法
    NSLog(@"model类中的编码");
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
    [aCoder encodeObject:self.data forKey:@"data"];
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        /**
         *  解码的过程 跟编码一样 除了类型以外 也是有 特殊解码方法
            注意: 编码的时候给的key 和解码的时候给的key 是一样的
         */
        NSLog(@"model类中的解码");
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeIntegerForKey:@"age"];
        self.data = [aDecoder decodeObjectForKey:@"data"];
    }
    return self;
}

@end
// 归档复杂对象
- (void)archiver
{
    // 初始化要归档的对象
    JJModel *model = [[JJModel alloc]init];
    // 赋值对象
    model.name = @"JJ";
    model.age = 60;
    // 比如搞一个图片作为data
    UIImage *image = [UIImage imageNamed:@"123"];
    model.data = UIImagePNGRepresentation(image);
   
   
    // 创建一个可变的data 进行初始化归档对象
    NSMutableData *modelData = [NSMutableData data];
    // 创建一个归档对象
   
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:modelData];
   
    NSLog(@"归档前");
    // 进行归档编码
    [archiver encodeObject:model forKey:@"JJmodel"];
    NSLog(@"归档后");
    // 编码完成
    [archiver finishEncoding];
   
   
    // 实际上归档 相当于把编码完的对象 保存到data 中

// NSLog(@"%@", modelData);
   
    // 把存有复杂对象的data 写入文件 持久化
   
    /**
     *  1. 路径
     *
     *  2. 调写入的方法
     */
    NSLog(@"%@", kDocumentsPath);
    NSLog(@"存入文件夹");
    NSString *modelDataPath = [kDocumentsPath stringByAppendingPathComponent:@"JJModel.da"];
    [modelData writeToFile:modelDataPath atomically:YES];
    [archiver release];

[model release];

}

// 反归档(解码的过程)
- (void)unArchiver
{
    // 获取刚才归档的data
    NSString *modelDataPath = [kDocumentsPath stringByAppendingPathComponent:@"JJModel.da"];
    NSData *data = [NSData dataWithContentsOfFile:modelDataPath];
     NSLog(@"%@", kDocumentsPath);
    // 创建反归档的对象
    NSKeyedUnarchiver *unAchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
   
    // 解码 返回一个对象
    NSLog(@"解码前");

JJModel *model = [unAchiver decodeObjectForKey:@"JJmodel"];
    NSLog(@"解码后");

// 反归档完成
    [unAchiver finishDecoding];
    // 释放对象
    [unAchiver release];
   
    NSLog(@"%@", model.name);
   
    UIImage *image = [UIImage imageWithData:model.data];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.frame = CGRectMake(0, 0, 375, 667);
    [self.view addSubview:imageView];

}    
   
5. 拓展,UIAlertView 三秒钟后自己消失 调用一个方法
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"message:@"收藏成功"delegate:selfcancelButtonTitle:nilotherButtonTitles:nil, nil];
    [alert show];
    [self performSelector:@selector(action:) withObject:alert afterDelay:0.3];
    [alert release];
- (void)action:(UIAlertView *)alert
{
    [alert dismissWithClickedButtonIndex:0 animated:YES];
}

UI一揽子计划 18 (沙盒机制、简单对象写入文件、NSFileMange、复杂对象写入文件)相关推荐

  1. Mac 开发(一) 苹果沙盒机制sandbox 简介

    文章目录 Mac 开发(一) 苹果沙盒机制sandbox简介 1 Mac sandbox简介 1.1 关于应用程序沙盒 1.2 为啥要用沙盒机制 2 沙盒原理 3 xcode中开启沙盒权限 3.1 X ...

  2. iOS开发之沙盒机制(SandBox)

    iOS APP可以在自己的沙盒里读写文件,但是,不可以访问其他APP的沙盒.每一个APP都是一个信息孤岛,相互是不可以进行通信的,唯独可以通过URL Scheme.沙盒里面的文件可以是照片.声音文件. ...

  3. IOS沙盒机制(SandBox)

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  4. Mac 开发(一) 苹果沙盒机制sandbox简介

    Mac 开发(一) 苹果沙盒机制sandbox简介 孔雨露正在上传-重新上传取消 2020年06月07日 16:41 ·  阅读 7332 @[TOC] Mac 开发(一) 苹果沙盒机制sandbox ...

  5. iOS 开发之沙盒机制 文件操作 (NSFielManager)

    原文链接:http://www.jianshu.com/p/349855b5a8ae iOS APP 可以在自己的沙盒里读写文件,但是,不可以访问其他 APP 的沙盒.每一个 APP 都是一个信息孤岛 ...

  6. iOS开发:沙盒机制以及利用沙盒存储字符串、数组、字典等数据

    iOS开发:沙盒机制以及利用沙盒存储字符串.数组.字典等数据 1.初识沙盒:(1).存储在内存中的数据,程序关闭,内存释放,数据就会丢失,这种数据是临时的.要想数据永久保存,将数据保存成文件,存储到程 ...

  7. 产品经理们是如何越过 iOS 沙盒机制的?

    点击上方篮字,轻松关注! 小咖导读 产品经理们是如何越过 iOS 沙盒机制的?今天PMcaff小咖就带大家来看看东方产品汪的暴力美学. iOS 沙盒机制 先来解释一下什么是 iOS 沙盒机制. iOS ...

  8. ios keychain 不被清理_苹果手机卸载软件会不会有残留?带你认识iOS沙盒机制!...

    iPhone能干净的卸载软件吗,会不会有残留_苹果手机怎么样完全清除软件残留个人信息? 这种问题,最容易发生在你换手机的时候,旧手机给家人使用或者是当二手机出给别人,就要使用到这种方法! 首先得了解下 ...

  9. macOS - Cocoa开发之沙盒机制及访问Sandbox之外的文件

    原文地址:http://www.skyfox.org/cocoa-macos-sandbox.html iOS默认并且只能读写对应的沙盒目录. OSX自从10.6系统开始引入沙盒机制,规定发布到Mac ...

最新文章

  1. 从Java 9 到 Java 17之Java 12
  2. C语言 位移运算符的使用
  3. form select multiple 某个字段是数组_你知道什么是Select函数吗?
  4. GRPC java实现demo
  5. 《ASCE1885的源码分析》の基于完成端口模型的TCP服务器框架
  6. Django 中间件
  7. [BZOJ 4827][Hnoi2017]礼物
  8. 每天一道LeetCode-----顺时针旋转n×n矩阵90度
  9. IOS遍历未知对象属性、函数
  10. 品质主管每日工作需要做哪些_做微信社群运营需要用到哪些工具来铺助工作呢?...
  11. android java标准时间_java android中对list的时间进行排序
  12. 小毛驴走呀走的openeim001
  13. fragment photoshop_史上最接地气的Photoshop?谈PS 2021的黑科技 - Adobe
  14. 刚刚,任正非为姚安娜商标事件道歉
  15. 高阶函数-语法糖-lambda(三分钟读懂)
  16. CentOS hadoop 伪分布式安装步骤
  17. 易筋SpringBoot 2.1 | 第十一篇:SpringBoot使用actuator
  18. ubuntu如何查看java版本_Ubuntu 如何查看安装的 JDK
  19. 关于光纤宽带技术,看这一篇就够啦!
  20. Luogu P4727 [HNOI2009]图的同构记数

热门文章

  1. Python分析了5万条相亲网站数据 | 看相亲男女画像
  2. Hibernate三种数据持久状态:临时态、游离态、持久化状态
  3. Hibername三种状态(瞬时态 持久态 游离态)
  4. 燕京理工学院java期末_2020-2021年燕京理工学院寒假放假时间安排及校历开学时间...
  5. director.js:客户端的路由---简明中文教程
  6. android videoview开发播放比例16比9,VideoView按原始視頻比例播放
  7. python语言源程序文件扩展名_c语言源程序的扩展名是什么
  8. 蛋白质结构预测---残基接触的基础知识---接触概念(三)
  9. 【Java 8 新特性】Java Clock 详解
  10. 查看电脑使用记录的命令