本地存数(数据持久化)主要是使用了沙盒
沙盒: 沙盒是iPhone工程进行文件操作有此工程对应的指定的位置, 不能逾越. 其中包含了四个文件: " documents, tmp, app, Library". 手动保存文件在documents文件夹里, NSUserDefaults保存的文件在tmp文件夹里.
1.获取sandBox下的文件夹路径:
/* 获取Documents文件夹路径 */
/* 使用函数:NSSearchPathForDirectoriesInDomains
* 参数1: 所要查找的目录名字
* 参数2: 所要搜索的域(范围), iOS 在NSUserDomainMask查找
* 参数3: YES(显示为绝对路径), NO(显示为相对路径 ~/)
* 函数返回类型是一个数组
*/
NSArray *documentsArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
/* 打印数组 */
NSLog(@"%@", documentsArr);
/* 从数组中将路径取出来 */
NSString *documentsPath = [documentsArr lastObject];
/* 打印字符串 */
NSLog(@"%@", documentsPath);
/* 获取Library 文件夹路径 */
NSArray *LibraryArr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *LibraryPath = [LibraryArr lastObject];
NSLog(@"%@", LibraryPath);
/* 获取tmp文件夹路径
* 使用 NSTemporaryDirectory() 函数
*/
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"%@", tmpPath);
/* 获取缓存文件夹路径 */
NSArray *CacherArr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *CacherPath = [CacherArr lastObject];
NSLog(@"%@", CacherPath);
/* 连写 */
NSString *cacherPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"%@", cacherPath);
a. NSString 字符串写入文件
/* 1. 要存入本地的字符串 */
NSString *str = @"大连";
/* 2. 获取要保存的路径(哪一个文件夹), 文件保存到Documents文件夹中 */
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
/* 3. 创建文件的路径
* 在docPath路径基础上拼接文件的名字
*/
/* 方法一: */
NSString *strPath = [docPath stringByAppendingString:@"/location.txt"];
/* 方法二: 不用输入前面的"/"符号, 会自动补全 */
NSString *strPath2 = [docPath stringByAppendingPathComponent:@"location.txt"];
NSLog(@"%@", strPath2);
/* 4. 将字符串写入文件 */
/*
* 参数1: 文件路径
* 参数2: 原子性, YES(保存时, 先保存在一个辅助文件中, 然后辅助文件替换原文件). NO(保存时, 直接写入原文件)
* 参数3: 编码标准
* 参数4: 报错信息
*/
NSError *error = nil;
[str writeToFile:strPath2 atomically:YES encoding:NSUTF8StringEncoding error:&error];
b. NSArray 写入文件
/* 1. 要保存的数组 */
NSArray *arr =@[@"大连", @"沈阳", @"北京"];
/* 2. 创建文件路径
* 数组存入本地, 若文件名没有后缀, 默认是XML格式
*/
NSString *arrayPath = [docPath stringByAppendingPathComponent:@"newArr.plist"];
/* 3. 写入 */
[arr writeToFile:arrayPath atomically:YES];
// NSLog(@"%@", arrayPath);
c. NSDictionary 存入本地
/* 1. 要保存的字典 */
NSDictionary *dic = @{@"name" : @"zhansan",
@"age" : @"18"};
/* 2. 创建文件路径 */
NSString *dicPath = [docPath stringByAppendingPathComponent:@"newDic.plist"];
/* 3. 写入 */
[dic writeToFile:dicPath atomically:YES];
// NSLog(@"%@", dicPath);
d. NSData 存入本地
/* 如何将图片保存到本地
* 思路: 首先将图片转化为NSData对象, 然后将NSData对象保存到本地中
* 使用函数将jpg图片转化为NSData, 函数中第二个参数是压缩比, 范围 0 - 1 , 1 压缩比最小图像质量最高, 0压缩比最大图像质量最低
*/
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"0.jpg"], 1);
/* 2. 创建文件路径 */
NSString *imagePath = [docPath stringByAppendingPathComponent:@"image.da"];
/* 3. 写入文件 */
[imageData writeToFile:imagePath atomically:YES];
NSLog(@"%@", imagePath);
3. 从文件中读取对象(即读文件)
/* 1. 找到文件路径(不是文件夹路径) */
NSString *documentsPath2 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *readStrPath = [documentsPath2 stringByAppendingPathComponent:@"location.txt"];
/* 2. 创建对象(即读出内容) */
NSString *readStr = [NSString stringWithContentsOfFile:readStrPath encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", readStr);
/* 数组 */
NSString *readArrPath = [documentsPath2 stringByAppendingPathComponent:@"newArr.plist"];
NSArray *readArr = [NSArray arrayWithContentsOfFile:readArrPath];
NSLog(@"%@", readArr);
/* 字典 */
NSString *readDicPath = [documentsPath2 stringByAppendingPathComponent:@"newDic.plist"];
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:readDicPath];
NSLog(@"%@", readDic);
/* NSData */
NSString *readImage = [documentsPath2 stringByAppendingPathComponent:@"image.da"];
NSData *readImageData = [NSData dataWithContentsOfFile:readImage];
UIImage *Image = [UIImage imageWithData:readImageData];
NSLog(@"%@", Image);
UIImageView *imageView = [[UIImageView alloc] initWithImage:Image];
imageView.frame = CGRectMake(100, 100, 300, 300);
[self.view addSubview:imageView];
[imageView release];
4. NSFileManager
/* 用于文件夹的管理, 如: 创建文件夹等, 删除文件等 */
/* 1. 创建对象, 使用defaultManager 方法, 单例对象 */
NSFileManager *fileManager = [NSFileManager defaultManager];
/* 创建文件夹 ~/documents/file1 */
/* 创建文件夹路径 */
NSString *file1Dir = [documentsPath2 stringByAppendingPathComponent:@"file1"];
[fileManager createDirectoryAtPath:file1Dir withIntermediateDirectories:YES attributes:nil error:&error];
// NSLog(@"%@", file1Dir);
/* 向file1文件夹中写入一个文件 */
NSString *string3 = @"北京";
NSString *string3Path = [file1Dir stringByAppendingPathComponent:@"beijing.txt"];
[string3 writeToFile:string3Path atomically:YES encoding:NSUTF8StringEncoding error:&error];
/* 删除文件夹, 删除caches文件夹, caches文件夹删除之后, 如果向这个文件夹写入东西, 或者程序重新运行, 系统会自动创建caches文件夹 */
NSString *cacgesPath3 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
[fileManager removeItemAtPath:cacgesPath3 error:&error];
/* 查看某个文件夹中的内容 */
NSArray *contentArr = [fileManager contentsOfDirectoryAtPath:file1Dir error:&error];
NSLog(@"%@", contentArr);
5. 复杂对象写入文件
/* 定义一个Person类 */
Person *person1 = [[Person alloc] init];
person1.name = @"时琦狂三";
person1.sex = @"妹纸";
person1.age = @"不详";
person1.number = 10;
person1.address = @"dalian";
/* 归档 */
/*
* 1. 将复杂对象转化为NSData对象
* 2. NSData 写入文件中
*/
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *archiver1 = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
/* 归档方法, 将person1 对象 归档 */
[archiver1 encodeObject:person1 forKey:@"per"];
/* 归档完成 */
[archiver1 finishEncoding];
/* 创建一个保存文件的路径 */
NSString *perPath = [documentsPath2 stringByAppendingPathComponent:@"per.da"];
/* NSData对象写入文件 */
[data writeToFile:perPath atomically:YES];
/* 反归档(读取文件) */
/*
* 1. 读取NSData数据
* 2. 将NSData 数据转化为复杂对象
*/
NSData *perData = [NSData dataWithContentsOfFile:perPath];
/* 创建反归档类对象 */
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:perData];
Person *per = [unarchiver decodeObjectForKey:@"per"];
/* 反归档结束 */
[unarchiver finishDecoding];
/* 验证 */
NSLog(@"%@", per.name);
NSLog(@"%@", per.sex);
NSLog(@"%@", per.address);
NSLog(@"%@", per.age);
NSLog(@"%d", per.number);