本章博客我主要是把UI后期初级数据持久化的一些基本使用总结了下,这里主要分享了沙盒机制的概念,简单对象不同情况下的写入,复杂对象的写入,以及Data与二进制
之间相互转化的方法,希望可以帮到大家。
一.沙盒机制
1. 每个应用程序位于文件系统的严格限制部分
2. 每个应用程序只能在为该程序创建的文件系统中读取文件
3. 每个应用程序在ios系统内都放在了统一的文件目录下
4. 沙盒路径的位置的方法有两种:
(1).通常我们会通过Finder查找程序沙盒相对路径
~/Library/ApplicationSupport/iPhone Simulator
(2).通过代码查找程序沙盒相对路径
NSSearchPathForDirectoriesInDomains(NSSearchPathDirectorydirectory, NSSearchPathDomainMask domainMask, BOOL expandTilde)
参数1:(Document文件夹,tmp文件夹,Library文件夹)要搜索哪个文件夹
参数2:(固定为NSUserDomainMask)获得iphone用户文件夹
参数3:YES(绝对路径)NO(相对路径)
5.NSBundle 获取列表中的文件 获取Bundle里的文件,Bundle也有两个重点:
(1).每个应用程序文件夹内的app文件是什么
(2).每个应用程序文件夹内的app文件路径如何获取
//获取 NSBundle包的路径 列表的路径
NSString * bundlePath = [NSBundle mainBundle].resourcePath;
NSLog(@"===========%@",bundlePath);
//获取Bundle包中图片的路径 列表中图片的路径
NSString * imgPath = [[NSBundle mainBundle] pathForResource:@"12" ofType:@"jpg"];
NSLog(@"=================%@",imgPath);
//通过文件路径产生一个UIimage
//好处:1.稳定 2.直接从文件读取(文件->内存)只走一次
//imageNamed:1.不稳定 2.适合多次使用(文件->内存)可以重复使用
UIImage * image = [UIImage imageWithContentsOfFile:imgPath];
二.简单对象写入文件
在写入文件的时候我们基本会遇到三种情况首先是要对文件储存,记下来对字符串,数组,字典三种情况分别写入。
1.文件存储的相对目录
NSArray * array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * path = [array objectAtIndex:0];
NSLog(@"=====%@",path);
2.字符串对象写入文件
//1.把字符串对象写入documents文件夹内
//第一步,给要写入的字符串指定一个文件的路径
NSString * txtPath = [path stringByAppendingString:@"/sss.txt"];
//第二部,创建一个字符串
NSString * string = @"何必要在一起";
//第三步,将字符串写入路径
//参数1:是文件路径 参数2:yes先放入缓存文件夹中
[string writeToFile:txtPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
3.数组对象写入文件
//数组写入路径(arr.plist 或者arr.XML)
NSString * arrPath = [path stringByAppendingString:@"/arr.XML"];
NSArray * arr = [NSArray arrayWithObjects:@"fldksj",@"sdfs",@"zisfj", nil];
[arr writeToFile:arrPath atomically:YES];
4.字典对象写入文件
//字典
NSString * dicPath = [path stringByAppendingPathComponent:@"dic.plist"];
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"fwfe",@"sddsg", nil];
[dic writeToFile:dicPath atomically:YES];
除了以上三种情况外,当我们遇到Data的时候要用二进制对象写入文件,个人感觉这个方法很重要!
int index = 0;
NSData * data = [[NSData alloc] initWithBytes:&indexlength:sizeof(index)];
NSString * dataPath =[path stringByAppendingPathComponent:@"data.aa"];
[data writeToFile:dataPath atomically:YES];
三.复杂对象写入文件
我们在对复杂对象写入之前,要明确知道什么是复杂对象
复杂对象是指在Foundation框架内不存在的数据类,无法在程序内通过write ToFile类型的方法写入到文件内,复杂对象至少包含一个实例对象
构造复杂对象的生成类
#import <Foundation/Foundation.h>
@interface Boss : NSObject<NSCoding>
@property (nonatomic,retain)NSString * name;
@property (nonatomic,retain)NSString * job;
@end
3.使复杂对象接受NSCoding协议
@interface Boss : NSObject<NSCoding>
4.使复杂对象实现NSCoding协议
#import "Boss.h"
@implementation Boss
//归档方法 将复杂的对象转换成数据流(NSData)的协议方法
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.name forKey:@"bossName"];
[aCoder encodeObject:self.job forKey:@"bossJob"];
}
//解档的方法 将NSData对象恢复成复杂对象的方法
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"bossName"];
self.job = [aDecoder decodeObjectForKey:@"bossJob"];
}
return self;
}
@end
5.将复杂对象Boss的实例对象写入文件
NSArray * libArr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString * libPath =[libArr lastObject];
NSLog(@"======%@",libPath);
//归档地点(构造复杂对象的存储路径)
NSString * bossPath = [libPath stringByAppendingPathComponent:@"boss.aa"];
Boss * boss = [[Boss alloc] init];
boss.name = @"蒋神";
boss.job = @"魔王";
//把复杂对象首先要转成NSData类型(归档)(通过将NSKeyedArchiver类对复杂对象进行序列化并且保存到文件路径下)
[NSKeyedArchiver archiveRootObject:boss toFile:bossPath];
NSLog(@"============%@",bossPath);
//反归档
Boss * bossReturn = [NSKeyedUnarchiver unarchiveObjectWithFile:bossPath];
NSLog(@"%@",bossReturn.name);