沙盒存储之路径

沙盒存储之路径

Documents:保存应用程序运行时生成的需要持久化的数据,iTunes会自动备份该目录

Library:存储程序的默认设置和其他状态信息,iTunes会自动备份该目录

Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录文件不会在应用退出删除。一般存放体积比较大的,不是特别重要的资源

Library/Preferences:保存应用的所有偏好设置,iTunes会自动备份该目录

tmp:保存应用运行时所需要的临时数据,使用完成后文件将相应的文件从该目录删除,iTunes不会同步该目录

地址是一个字符串

第一个参数:枚举值,枚举你具体要查找的文件夹【要进入哪个文件夹直接修改其枚举值即可】NSDocumentDirectory:进入Documents的文件夹

第二个参数:NSUserDomainMask表示用户的主目录

第三个参数:一般设置为YES表示展示完整的路径

NSSearchPathForDirectoriesInDomains 查找沙盒路径的,返回值是一个数组,数组就一个元素,根据下标取出即可

第一种方法 找到documents的路径

1 // 通过函数找到documents路径
2     NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
3 // 打印确认是否获取到路径
4     NSLog(@"documentPath = %@", documentPath);

第二种方法 找到Caches文件夹的路径

1 // 第一步:先找到主目录文件夹
2     NSString *homePath = NSHomeDirectory();
3     NSLog(@"homePath = %@", homePath);
4     
5 // 第二步:然后拼接自己想进入的文件夹名称
6     NSString *cachesPath = [homePath stringByAppendingPathComponent:@"Library/Caches"];
7     NSLog(@"cachesPath = %@", cachesPath);

注意:tmp的路径获取与其他的不同

1 // tem 有自己的查找函数
2     NSString *tmpPath = NSTemporaryDirectory();
3 // 打印
4     NSLog(@"tmpPath = %@", tmpPath);

简单对象的本地持久化

 1 #pragma mark - 简单对象的本地持久化
 2 #pragma mark - 将NSString类型的数据存到本地
 3 
 4     // 1> 需要知道这个对象存在哪里,所以需要一个文件夹的路径
 5     //    找到documents文件夹路径
 6     
 7     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 8     
 9     // 2> 我们知道要存储什么,所以需要创建什么
10     //    创建要存储的内容:字符串
11     
12     NSString *str = @"AJAR";
13     
14     // 3> 需要知道字符串最终存储的地方,所以需要创建一个路径去存储字符串
15     NSString *strPath = [documentsPath stringByAppendingPathComponent:@"wmw.txt"];
16     
17     // 4> 准备工作完成,将字符串写入文件
18     // 第一个参数:写入文件的一个路径
19     // 第二个参数:断电自动保存
20     // 第三个参数:编码格式
21     
22     [str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
23     
24 #pragma mark - 将NSString文件夹存储的内容取出
25     // 将字符串取出: 使用 stringWithContentsOfFile: 方法将其取出
26     // 第一个参数:字符串存储的路径
27     // 第二个参数:编写格式
28     // 第三个参数:错误信息
29     NSString *newStr = [NSString stringWithContentsOfFile:strPath encoding:NSUTF8StringEncoding error:nil];
30     
31     NSLog(@"%@", newStr);

NSString,NSArray,NSDictionary 这几个对象的本地持久化方法基本相同,只是创建存储内容的步骤不同,不在一一举例,下面说一下NSData

 1 #pragma mark - 将NSData类型的数据存储到本地(以图片为例)
 2     // 常用的两种初始化image的方式
 3     // UIImage *image = [UIImage imageNamed:<#(nonnull NSString *)#>];
 4     // UIImage *image = [[UIImage alloc] initWithContentsOfFile:<#(nonnull NSString *)#>];
 5     
 6     // 1、imageNamed 第一次读取的时候,先把图片放到缓存里,次啊次在使用的到这个同名图片的时候 直接从缓存中读取(优点:方便快捷,只是第一次使用的时候稍慢。 缺点:如果在当前工程中只是用一次会浪费内存)
 7     // 2、initWithContentsOfFile 初始化图片的时候,每次都会根据路径去读取,不会占用内存,如果图片在当前工程中只用一次,应该选择这个方法
 8     
 9     UIImage *image = [UIImage imageNamed:@"v_red_heart_selected"];
10     
11     /*
12      123.png
13      123@2x.png
14      123@3x.png
15      
16      图片适配的相关内容
17      */
18     
19     // 将image类型的对象转换成NSData类型的数据进行存储
20     // 使用UIImageJPEGRepresentation 将图片转换成NSData类型的
21     // 第一个参数:要转换的image对象
22     // 第二个参数:图片的压缩的数值
23     // iPhone中将大于2M的图片,会自动旋转90度,然后进行压缩处理
24     NSData *imageData = UIImageJPEGRepresentation(image, 1);
25     // 找到路径进行存储
26     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
27     // 最终路径
28     NSString *imagePath = [documentsPath stringByAppendingString: @"/123.jpeg"];
29     // 写入
30     [imageData writeToFile:imagePath atomically:YES];
31     NSLog(@"%@", imagePath);
32     
33     // 读取NSData类型的数据
34     // 需求:将NSData类型的数据读取出来,转换成UIImage类型的并显示在imageView上
35     NSData *newData = [NSData dataWithContentsOfFile:imagePath];
36     
37     UIImage *showImage = [[UIImage alloc] initWithData:newData];
38     
39     UIImageView *imageView = [[UIImageView alloc] initWithImage:showImage];
40     [self.view addSubview:imageView];

复杂数据本地化

 1     // 如何把一个Person对象存入本地,即为复杂对象的本地化,这个对象必须遵守NSCoding协议,并实现协议的两个方法
 2    // 创建Person类
 3 
 4 #pragma mark - 复杂对象的本地化
 5     
 6     // 1 找寻Documents文件夹目录
 7     NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
 8     // 2 创建要存储的对象
 9     Person *person = [[Person alloc] init];
10     person.name = @"MBBoy";
11     person.age = 99;
12     person.gender = @"F";
13     
14     // 3 把这个复杂对象归档
15     // 3.1 初始化NSMutableData,用于创建归档工具的
16     NSMutableData *data = [NSMutableData data];
17     // 3.2 创建归档工具
18     NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
19     // 3.3 对要归档的person对象进行归档
20     [archiver encodeObject:person forKey:@"person"];
21     NSLog(@"==== %@", data);
22     // 3.4 结束归档
23     [archiver finishEncoding];
24     
25     NSLog(@"==== %@", data);
26     // 4 将归档的内容NSMutableData存储在本地
27     NSString *personPath = [documentsPath stringByAppendingPathComponent:@"person.plist"];
28     [data writeToFile:personPath atomically:YES];
29     NSLog(@"==== %@", data);
30     
31     
32 #pragma mark - 解档
33     // 1 将要归档的数据找出
34     NSData *resultData = [NSData dataWithContentsOfFile:personPath];
35     NSLog(@"%@", resultData);
36     // 2 创建解档工具
37     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:resultData];
38     // 3 对要解档的对象进行解档 (使用oerson对象接收)
39     Person *newPerson = [unarchiver decodeObjectForKey:@"person"];
40     // 4 结束解档
41     [unarchiver finishDecoding];
42     NSLog(@"name = %@  age = %ld gender = %@", newPerson.name, newPerson.age, newPerson.gender);

  

原文地址:https://www.cnblogs.com/crazygeek/p/5456442.html