数据持久化(归档和反归档操作 ,清除缓存等等)
2015-08-19 19:55
567 查看
数据持久化的步骤
// 1.指定前往哪个文件夹 // 2,用字符串接收路径 // 3.拼接文件夹路径 // 4.写入本地或者归档操作 // 注 :如果是复杂对象归档 ,要签订NSCoding方法 .并且实现两个协议方法,放在数组里的复杂对象归档也要签协议
苹果手机为了保证自己数据上的绝对的安全设计了沙盒文件 ,每一个应用程序都配备了自己的沙盒文件 ,每一次运行 ,文件夹的名字就会变成一个没有任何规律的字符串
复杂对象写入到本地 ,主要至我们自己创建的对象写入到本地, 也叫归档和反归档操作
结构体 归档与反归档
创建对象 Student *stu =[[Student alloc] initWithName:@"11" hobby:@"11" sex:@"11" age:70]; 1.通过数组获取沙盒地址 NSArray *sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 2.用字符串保存沙盒路径 NSString *sandBoxPath =sandBox[0]; 3.拼接文件夹路径 ,这个文件的扩展名是任意的 NSString *documentPath =[sandBoxPath stringByAppendingPathComponent:@"学生.avi"]; 4.对对象进行归档操作 第一个参数 :要试试归档的对象 第二个参数 :路径 [NSKeyedArchiver archiveRootObject:stu toFile:documentPath]; 反归档 Student *newstu =[NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
数组归档
Student *stu =[[Student alloc] initWithName:@"11" hobby:@"11" sex:@"11" age:70]; Student *stu1 =[[Student alloc] initWithName:@"22" hobby:@"22" sex:@"nan" age:71]; Student *stu2 =[[Student alloc] initWithName:@"33" hobby:@"33" sex:@"nv" age:30]; NSMutableArray *arr =[NSMutableArray arrayWithObjects:stu,stu1,stu2, nil]; NSArray *sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *sandBoxPath =sandBox[0]; NSString *document=[sandBoxPath stringByAppendingPathComponent:@"xuesheng.avi"]; // 归档 [NSKeyedArchiver archiveRootObject:arr toFile:document]; NSLog(@"%@",document); // 反归档 NSArray *newArr =[NSKeyedUnarchiver unarchiveObjectWithFile:document]; for (Student *stu in newArr) { NSLog(@"%@",stu.name); }
NSUserDefaults 一般存放的实效的数据,比如字符串等,他用法和字典类似
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; [defaults setObject:@"12345" forKey:@"password"]; [defaults synchronize]; NSArray *sandBox =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *sandBoxPath =sandBox[0]; NSLog(@"%@",sandBoxPath); NSLog(@"%@", [defaults objectForKey:@"password"]);
通过文件管理者对文件进行操作
NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES); NSString *sandBoxPath=sandBox[0]; // 创建一个文件管理者 NSFileManager *manager =[NSFileManager defaultManager]; // 给要创建的文件夹拼接一个路径 NSString *filePath =[sandBoxPath stringByAppendingPathComponent:@"guyu"]; // 文件夹的名不需要有任何的扩展名 // 通过manager进行文件夹的创建 [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; // NSLog(@"%@",filePath); // 向新创建的文件夹里写入一个字符串 NSString *document =[filePath stringByAppendingPathComponent:@"guyu.txt"]; NSString *str =@"11"; [str writeToFile:document atomically:YES encoding:NSUTF8StringEncoding error:nil]; 移除文件夹 // [manager removeItemAtPath:filePath error:nil]; // NSLog(@"%@",sandBoxPath);
移除缓存文件
NSArray *scache= NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES); NSString *cachePath = scache[0]; [manager removeItemAtPath:cachePath error:nil]; NSLog(@"%@",cachePath);
相关文章推荐
- sqlserver 中一些常看的指标和清除缓存的方法
- 多种浏览器清除缓存的方法小结
- xmlhttp缓存清除的2种解决方法
- window.showModalDialog两次加载问题清除缓存方法
- Hibernate上路_12-一对多表单操作
- Hibernate上路_13-一对一表单操作
- Hibernate上路_14-多对多表单操作
- Hibernate上路_15-多对多表单操作
- Hibernate上路_16-继承关系映射
- Hibernate上路_17-检索策略
- Hibernate上路_18-Hibernate查询方式
- Hibernate上路_20-session管理
- Hibernate上路_21-二级缓存
- Hibernate上路_22-从表自动创建Java类
- iOS中常用的四种数据持久化方法简介
- 【Android】数据共享 sharedPreferences 相关注意事项
- 数据持久化之NSUserDefaults
- iOS数据持久化技术
- UIWebView清除缓存和cookie