标签:
数据持久化 :
数据持久化,实际上就是将数据存放到网络或者硬盘上,这里是存储到本地的硬盘上,应用程序的本地硬盘是沙盒,沙盒实际上就是一个文件夹,它下面有4个文件夹。分别是Documents,Library,APP包和tmp文件夹,Documents里面主要是存储用户长期使用的文件,Library里面又有Caches和Preferences文件夹,Caches里面存放的是临时的文件,缓存。Preferences里面存放的是偏好设置。tmp里面也是临时的文件,不过和Caches还有区别,APP包里面是编译后的一些文件,包不能修改。
沙盒机制:有4个文件夹
Document :存储用户数据,需要备份到信息
Library/Caches :存储缓存文件,程序专用的支持文件
Library/Preferences :存储应用程序的偏好设置文件
.app :程序包(iOS8时,app不存储在沙盒中,有单独的文件夹储存所有程序的app包)不能被修改
tmp :存储临时文件。比如下载包,压缩后的再删除
获取主要目录路径的方式
NSHomeDirectory() : 沙盒主路径
NSDocumentDirectory : Documents文件夹
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLibraryDirectory : Library文件夹
NSCachesDiectory : Caches文件夹
NSTemporaryDirectory() : tmp文件夹
Myapp.app : [[NSBundle mainBundle] bundlePath];
写入文件的方法
//获取document路径
NSArray * directories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docPath = [directories lastObject];
NSString * strPath = [docPath stringByAppendingPathComponent:@"/text.txt"];
NSString * content = @"xxx,赶紧把俯卧撑做了...";
[content writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
数组对象写入文件 (NSArray)
NSString * arrayPath = [docPath stringByAppendingPathComponent:@"array.txt"];
NSArray * array = @[@"张三",@"李四",@"王五"];
[array writeToFile:arrayPath atomically:YES];
字典对象写入文件 (NSDictionary)
NSString * dicPath = [docPath stringByAppendingString:@"/dic.txt"];
NSDictionary * dic = @{@"name":@"小黑",@"sex":@"男",@"age":@"22"};
[dic writeToFile:dicPath atomically:YES];
二进制对象写入文件 (NSData)
NSString * dataPath = [docPath stringByAppendingString:@"/data.txt"];
UIImage * img = [UIImage imageNamed:@"1.png"];
NSData * data = UIImagePNGRepresentation(img);
[data writeToFile:dataPath atomically:YES];
NSFileManager :文件管理,使用detaultManager创建单例
//创建文件管理对象
NSString * document = [docPath stringByAppendingString:@"/a/b/c"];
NSFileManager * manager = [NSFileManager defaultManager];
[manager createDirectoryAtPath:document withIntermediateDirectories:YES attributes:nil error:nil];
//创建文件text.txt
NSString * filePath = [docPath stringByAppendingString:@"/a/b/c/text.txt"];
[manager createFileAtPath:filePath contents:nil attributes:nil];
归档反归档
首先要遵循NSCoding协议,协议有2个方法
建一个类 3个属性
//归档是调用,对对象里面的属性/实例变量进行编码
- (void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeObject:_gender forKey:@"gender"];
[aCoder encodeInt:_age forKey:@"age"];
}
//反归档是调用,对属性/实例变量进行编码
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.gender = [aDecoder decodeObjectForKey:@"gender"];
self.age = [aDecoder decodeIntForKey:@"age"];
}
return self;
}
主程序中进行归档反归档
//1、定义NSMutable对象,用于存放归档后的二进制数据
NSMutableData * mulData = [NSMutableData data];
//2、定义归档工具类
NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mulData];
//3、对对象p进行编码(归档)
[archiver encodeObject:p forKey:@"person"];
//4、完成归档
[archiver finishEncoding];
//写入文件
NSString * docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
NSString * dataPath = [docPath stringByAppendingPathComponent:@"archiveData.data"];
[mulData writeToFile:dataPath atomically:YES];
//1、从文件中读取二进制数据
NSData * readData = [NSData dataWithContentsOfFile:dataPath];
//2、定义反归档工具类
NSKeyedUnarchiver * unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:readData];
//3、解码(反归档)
Person * resultP = [unarchiver decodeObjectForKey:@"person"];
//4、结束反归档
[unarchiver finishDecoding];
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
数据库
标签:
原文地址:http://www.cnblogs.com/wypdahuo/p/4938460.html