沙盒(sandBox)
1.沙盒机制:它是一种安全体系,iOS应用程序只能对自己创建的程序进行读取文件,这个独立,封闭,安全的空间,就叫沙盒。它里面一般存放着程序需要的文件,数据持久化的一些文件,只要不是代码相关的都会放在里面。
2.每个一个用程序只有一个沙盒,沙盒有三个文件夹
➕:获取根目录的方式
NSString *homePath = NSHomeDirectory();
1⃣️:Documents 目录下的内容会被同步到另一台设备,可以存放媒体资源,文本资源
➕:获取Documents的目录
NSArray *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPaths = [documentsPath firstObject];
NSLog(@"%@",documentsPaths);
2⃣️:Library 可以把缓存的内容放到这里面 Library/Caches
➕:获取Library的方式
NSArray *librayPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryPsths = [librayPath lastObject];
NSLog(@“%@",libraryPsths);
➕:获取Caches的目录
NSArray *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesPaths = [cachesPath firstObject];
NSLog(@"%@",cachesPaths);
3⃣️: tmp 这是一个临时目录,当我们的设备重启时,文件会被自动清除
➕:获取tmp的目录
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@",tmpPath);*/
归档
1.归档: NSKeyedArchiver 又叫序列化(Coding) 归档后的文件会被加密
2.可以归档的文件、数据:任何数据,文件(直接归档一个类实例化出来的对象,数组,字典,字符串,文本,图片。。。。)
3.归档步骤:
1⃣️: 归档路径,需要归档的数据
2⃣️:归档
➕:归档实例:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath =[ [path firstObject]stringByAppendingPathComponent:@“userList.archiver”];
// 前两句是文件的路径
NSArray *users = @[@"葫芦娃",@"喵喵",@"汪汪"];
BOOL success = [NSKeyedArchiver archiveRootObject:users toFile:documentPath];
// 归档
if (success) {
[self showAlertWithMessage:@"归档成功"];
}
NSLog(@"%@",anotherPath);
4.解归档步骤:
解档(解归档):NSKeyedUnarchiver
1⃣️:解归档的文件路径
2⃣️:解归档
➕:解归档实例
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path1 = [[paths lastObject] stringByAppendingPathComponent:@“userList.archiver"];
// 解归档文件路径
NSArray *ary = [NSKeyedUnarchiver unarchiveObjectWithFile:path1];
// 解归档
第二种归档方式
NSData : 二进制数据的类
1.归档步骤:
1⃣️:归档文件的路径,准备需要归档的数据
2⃣️:写一个可变的NSMutableData 通过归档的类,让NSData准备写入到文件中
3⃣️:开始归档(编码 Coding)把Data数据写到文件中
➕:归档实例
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *anotherPath =[[path lastObject] stringByAppendingPathComponent:@“userList1.archiver"];
// 上两句是文件路径
1. 归档文件的路径 准备需要归档的数据
NSArray *ray = @[@"123",@"23",@"34"];
NSDictionary *dic = @{@"1":@"A",@"2":@"B",@"3":@"C"};
2.写一个可变的NSMutableData 通过归档的类 让NSData准备写入到文件中
NSMutableData *data = [[NSMutableData alloc]init];
NSKeyedArchiver *archiver =[[NSKeyedArchiver alloc]initForWritingWithMutableData:data];// 准备写入文件
3.开始归档(编码) 编码的key 和 解码的时候的key 一定要统一
[archiver encodeObject:ray forKey:@"ray"];
4.结束归档 编码 将Data数据写入到文件中
[archiver finishEncoding];
BOOL success = [data writeToFile:anotherPath atomically:YES];//在写入数据时 如果出现意外情况(用户突然强制退出了应用程序,断电....)会把写入到临时文件的内容清除掉 不再写入到目标文件,如果atomically是NO,不管数据是否完整都会直接写入到目标文件
if (success) {
[self showAlertWithMessage:@"归档成功"];
}
2.解归档步骤:
1⃣️:文件路径
2⃣️:读取文件里面的Data数据
3⃣️:通过解归档对象
4⃣️:解归档数据
➕:解归档实例
1.文件路径
/* NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [[path firstObject] stringByAppendingPathComponent:@"userList1.archiver"];
// 2.读取文件里面的Data数据
NSData *data = [NSData dataWithContentsOfFile:paths];
// 3.通过解归档对象
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
// 4.解归档 解码
NSArray *arr =[unarchiver decodeObjectForKey:@"ray"];
NSDictionary *dii = [unarchiver decodeObjectForKey:@"dic"];
NSLog(@"%@ %@",arr,dii);
// [unarchiver decodeObjectForKey:@"ray"];
归档自定义类的对象
条件:
1⃣️:遵守归档协议NSCoding
2⃣️:实现归档协议里面的方法
3⃣️:逐一对自定义类里面的属性进行编码和解码,这个对象就具备了归档和解归档的功能了
4⃣️:开始归档 解归档
➕:归档实例
// 要归档userModel里面的 name、age属性内容
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [[path firstObject]stringByAppendingPathComponent:@"userModel.archiver"];
userModel *model = [[userModel alloc]init];
model.name = @"葫芦娃";
model.age = 18;
BOOL success = [NSKeyedArchiver archiveRootObject:model toFile:paths];
if (success) {
[self showAlertWithMessage:@"归档成功"];
NSLog(@"%@,%ld",model.name,(long)model.age);
}
// 对已经归档的userModel里面的name、age属性解归档
➕:解归档实例
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *paths = [[path firstObject]stringByAppendingPathComponent:@"userModel.archiver"];
userModel *p = [NSKeyedUnarchiver unarchiveObjectWithFile:paths];
NSLog(@"%@,%ld",p.name,(long)p.age);