什么是数据持久化?数据的永久存储
为什么要坐数据持久化:存储在内存中的数据,程序关闭,内存释放,数据丢失,这种数据是临时的
数据初九化的本质:数据保存成文件,存储到程序的沙河中
1.沙盒机制
每个应用程序位于文件系统的严格限制部分
每个应用程序只能在为该程序创建的文件系统中读取文件
每个应用程序在IOS系统内都放在了统一的文件夹目录下
沙盒的本质就是一个文件夹,名字是随机分配的.
2.沙盒路径的位置
1.通过Finder查找程序沙盒相对的路径
通过代码查找程序沙盒相对路径
NSString *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",DocumentPath);
2.沙盒构成
Document 存储用户数据,需要备份的信息
Library/Caches 存储缓存文件,程序专用的支持文件
Library/Preferences 存储应用程序的偏好设置文件
.app 程序包(IOS8时,app不存储在沙盒中,有单独的文件夹存储所有程序的app包)
tmp 存储临时文件,比如:下载的zip包,解压后的再删除
3.获取沙盒目录路径的方法
NSHomeDirectory-------------------->沙盒主路径
NSDocumentDirectory--------------->Document文件夹
NSLibraryDirectory------------------->Library文件夹
NSCachesDirectory------------------>Caches文件夹
NSTemporaryDirectory--------------->tem文件夹
代码
<span style="font-family:SimHei;font-size:18px;">//1.home主目录里面有:Documents,Library,tmp和一个应用程序 NSLog(@"Home:%@",NSHomeDirectory()); //2.DocumentsPath路径 NSString *DocumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"DocumentsPath:%@",DocumentsPath); //3.Libray NSString *librayPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",librayPath); //4.temp NSLog(@"temp:%@",NSTemporaryDirectory()); //5.cachesPath NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]; NSLog(@"cachesPath:%@",cachesPath); //6.user NSString *user = NSUserName(); NSLog(@"user:%@",user);</span>
<span style="font-family:SimHei;font-size:18px;">//nsstring写入 //1.写入的路径 NSString *DocumentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; NSLog(@"%@",DocumentPath); //2.拼接文件路径 NSString *filePath = [DocumentPath stringByAppendingString:@"/myText.txt"]; //3.准备写入的内容 NSString *content = @"Hello World"; [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil]; //4.读取 NSString *readString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; NSLog(@"redstring : %@",readString); //NSArray //1.获取documents路径 //2.拼接文件路径 NSString *arrayFile = [DocumentPath stringByAppendingString:@"/array.plist"]; NSLog(@"arrayPath = %@",arrayFile); //3.准备内容 NSArray * contentArray = @[@"1",@"2",@"3",@"4",@"5",]; //4.写入 [contentArray writeToFile:arrayFile atomically:YES]; //5.读取 NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayFile]; NSLog(@"readArray : %@",readArray); //dictinary //1.拼接 NSString *dictFile = [DocumentPath stringByAppendingString:@"/dict.plist"]; //2.准备内容 NSDictionary *dictcontent = @{@"1":@"a",@"2":@"b",@"3":@"c"}; NSLog(@"%@",dictFile); //3.写入 [dictcontent writeToFile:dictFile atomically:YES]; //4.读取字典 NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:dictFile]; NSLog(@"dict : %@",readDict);</span>-------------------------------->>>NSFileManager
NSFileManager,文件管理,使用detaultManager,创建单利对象
可以创建文件夹
可以创建,移动,复制,删除文件,
可以判断文件是否存在
<span style="font-family:SimHei;font-size:18px;"> //NSFileManager //创建文件夹 //在Documents中创建一个文件夹 NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //在Documents中创建一个文件夹命名为"个人收藏" NSString *path = [documentsPath stringByAppendingString:@"/个人收藏"]; //创建文件管理(单利),并创建文件夹 [[NSFileManager defaultManager]createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil]; NSLog(@"DocumentsPath%@",documentsPath); //修改文件夹 NSString *newPath = [documentsPath stringByAppendingString:@"/岛国文化"]; [[NSFileManager defaultManager]moveItemAtPath:path toPath:newPath error:nil]; //删除 [[NSFileManager defaultManager]removeItemAtPath:documentsPath error:nil]; //判断某个文件是否存在 //返回值是BOOL,YES存在,NO不存在 [[NSFileManager defaultManager]fileExistsAtPath:newPath];</span>
1.什么是复杂对象
1.在foundation框架内不存在的数据类
2.无法在程序内通过writeToFile类型的方法写入到文件内
3.复杂对象至少包含一个实例对象
复杂对象无法通过writeToFile:方法进行数据持久化,只能通过将复杂对象转换为NSData,通过writeToFile进行数据持久化
将复杂的对象转化为NSData,通过归档;将NSData转换为复杂对象,通过反归档
复杂对象写入文件要遵循NSCoding协议
有两个方法,代码如下:
<span style="font-family:SimHei;font-size:18px;">//进行归档时调用(系统调用) -(void)encodeWithCoder:(NSCoder *)aCoder { //对属性进行编码 [aCoder encodeObject:self.name forKey:kName]; [aCoder encodeObject:self.age forKey:kAge]; } //进行反归档编码时 -(id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; //反编码 if (self) { self.name = [aDecoder decodeObjectForKey:kName]; } return self; }</span>创建一个Person类
归档/反归档代码如下:
<span style="font-family:SimHei;font-size:18px;">//归档 反归档 //创建 Person类实例对象 Person *person1 = [[Person alloc] init]; person1.name = @"刘杰"; person1.age = @"39"; Person *person2 = [[Person alloc] init]; person2.name = @"李士杰"; person2.age = @"18"; //归档使用的NSData NSMutableData *Person1Data = [NSMutableData data]; //创建归档工具 NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:Person1Data]; //进行归档 [archiver encodeObject:person1 forKey:kPerson1]; [archiver encodeObject:person2 forKey:kPerson2]; //完成转换 [archiver finishEncoding]; //找到路径 NSString *docunment = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //拼接文件路径 NSString *personPath = [docunment stringByAppendingString:@"/刘杰.xxoo"]; //写入文档 [Person1Data writeToFile:personPath atomically:YES]; NSLog(@"%@",docunment); //反归档 //通过文件路径,获取data数据 NSData * unData = [NSData dataWithContentsOfFile:personPath]; //反归档工具 NSKeyedUnarchiver *unAechiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:unData]; //反归档 Person *p1 = [unAechiver decodeObjectForKey:kPerson1]; Person *p2 = [unAechiver decodeObjectForKey:kPerson2]; //结束反归档 [unAechiver finishDecoding]; NSLog(@"name:%@",p1.name); NSLog(@"name :%@",p2.name); </span>
单个归档.反归档
<span style="font-family:SimHei;font-size:18px;"> //获取Documents路径 NSString *DocumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //拼接文件路径 NSString *filePath = [DocumentsPath stringByAppendingString:@"/PersonArray.plist"]; //实例一个对象 Person *p1 = [[Person alloc] init]; p1.name = @"别闹了"; p1.age = @"1"; //归档 [NSKeyedArchiver archiveRootObject:p1 toFile:filePath]; //反归档 Person *p2 = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"name: %@",p2.name);</span>
<span style="font-family:SimHei;font-size:18px;"> //获取Documents路径 NSString *DocumentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; //拼接文件路径 NSString *filePath = [DocumentsPath stringByAppendingString:@"/PersonArray.plist"]; //实例一个对象 Person *pn1 = [[Person alloc] init]; pn1.name = @"TOM"; pn1.age = @"12"; Person *pn2 = [[Person alloc] init]; pn2.name = @"KIM"; pn2.age = @"18"; NSArray *array = @[pn1,pn2]; //归档 [NSKeyedArchiver archiveRootObject:array toFile:filePath]; //反归档 NSArray *a = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"%@,%@",[a[0] name],[a[1] name]);</span>
沙盒机制:
简单对象写入文件,只能是NSString,NSArray,NSDictionary,NSData
复杂的对象写入文件,遵守NSCoding协议,实现代理方法
原文:http://blog.csdn.net/shichangbu123/article/details/41287281