数据存储之归档
2023-09-14 08:57:58 时间
在使用plist进行数据存储和读取,只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦;
偏好设置(将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息)
归档:因为前两者都有一个致命的缺陷,只能存储常用的类型。归档可以实现把自定义的对象存放在文件中。
先看下项目的结构 Student类继承Person类
在ViewController.m中
[aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"]; [aCoder encodeObject:self.photo forKey:@"photo"]; - (id)initWithCoder:(NSCoder *)aDecoder if (self=[super init]) { NSLog(@"person encodeWithCoder"); _age=[aDecoder decodeIntegerForKey:@"age"]; _name=[aDecoder decodeObjectForKey:@"name"]; _photo=[aDecoder decodeObjectForKey:@"photo"]; return self; @end 在Student类中继承Person类 增加了一个学号属性
// Created by City--Online on 15/4/22. // Copyright (c) 2015年 CYW. All rights reserved. #import "Student.h" @implementation Student - (void)encodeWithCoder:(NSCoder *)aCoder [super encodeWithCoder:aCoder]; NSLog(@"student encodeWithCoder"); [aCoder encodeObject:self.stuId forKey:@"stuId"]; - (id)initWithCoder:(NSCoder *)aDecoder if (self=[super initWithCoder:aDecoder]) { NSLog(@"student initWithCoder"); _stuId=[aDecoder decodeObjectForKey:@"stuId"]; return self; @end
储留香:天庭需要怎样的数据存储系统 在中国神话故事中,高达九重的天庭之中神仙众多,二十八星宿、三十六天罡、七十二地煞等等,每位大仙也多是长生不死,少则几百、多则几万岁,要保障如此庞大的系统组织高效运转就需要一个良好的数据存储系统。
2、设置归档存放路径(只可以设置其一,不可以并行) a) log_archive_dest:指定归档文件存放的路径,该路径只能是本地磁盘,默认为 ,不能与以下参数同使用。
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.
// // ViewController.m // Archive // Created by City--Online on 15/4/22. // Copyright (c) 2015年 CYW. All rights reserved. #import "ViewController.h" #import "Student.h" @interface ViewController () @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSArray *documents=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentpath=[documents firstObject]; // NSLog(@"%@",documentpath); NSString *filePath=[documentpath stringByAppendingPathComponent:@"CYW"]; //使用archiveRootObject简单归档 单个对象 //缺点:只能把一个对象归档到一个文件 #if 0 NSArray *arr=[[NSArray alloc]initWithObjects:@"1",@"2",@"3", nil]; //归档 //可对字典、数组、字符串、数字等进行归档 返回bool值表示是否归档成功 [NSKeyedArchiver archiveRootObject:arr toFile:filePath]; #endif #if 0 //解挡 NSArray *arr=[NSKeyedUnarchiver unarchiveObjectWithFile:filePath]; NSLog(@"%@",arr); #endif //多个对象归档 #if 0 //准备数据 CGPoint point=CGPointMake(100, 100); NSString *mystring=@"cuiyanwei"; BOOL YesOrNo=YES; //归档 NSMutableData *mutableData=[[NSMutableData alloc]init]; NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData]; [archiver encodeCGPoint:point forKey:@"mypoint"]; [archiver encodeBool:YesOrNo forKey:@"mybool"]; [archiver encodeObject:mystring forKey:@"mystring"]; [archiver finishEncoding]; [mutableData writeToFile:filePath atomically:YES]; #endif #if 0 //多对象解档 NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath]; NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData]; CGPoint point=[unarchiver decodeCGPointForKey:@"mypoint"]; BOOL mybool=[unarchiver decodeBoolForKey:@"mybool"]; NSString *mystring=[unarchiver decodeObjectForKey:@"mystring"]; [unarchiver finishDecoding]; NSLog(@"X=%lf,Y=%lf\n mybool=%d\n mystring=%@",point.x,point.y,mybool,mystring); UIImage #endif #if 0 //自定义对象归档 Student *stu1=[[Student alloc]init]; stu1.name=@"cuiyanwei"; stu1.age=24; stu1.photo=[UIImage imageNamed:@"email.png"]; stu1.stuId=@"001"; Student *stu2=[[Student alloc]init]; stu2.name=@"xiaocui"; stu2.age=23; stu2.photo=[UIImage imageNamed:@"email.png"]; stu2.stuId=@"002"; //归档 NSArray *array=[[NSArray alloc]initWithObjects:stu1,stu2, nil]; NSMutableData *mutableData=[[NSMutableData alloc]init]; NSKeyedArchiver *archiver=[[NSKeyedArchiver alloc]initForWritingWithMutableData:mutableData]; [archiver encodeObject:array forKey:@"students"]; [archiver finishEncoding]; [mutableData writeToFile:filePath atomically:YES]; #endif #if 1 //解档 NSMutableData *mutableData=[[NSMutableData alloc]initWithContentsOfFile:filePath]; NSKeyedUnarchiver *unarchiver=[[NSKeyedUnarchiver alloc]initForReadingWithData:mutableData]; NSArray *array=[unarchiver decodeObjectForKey:@"students"]; for (Student *stu in array) { NSLog(@"name=%@ age=%ld photo=%@ stuId=%@",stu.name,stu.age,stu.photo,stu.stuId); #endif - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. @end在Person.h中定义了几个属性
[aCoder encodeObject:self.name forKey:@"name"]; [aCoder encodeInteger:self.age forKey:@"age"]; [aCoder encodeObject:self.photo forKey:@"photo"]; - (id)initWithCoder:(NSCoder *)aDecoder if (self=[super init]) { NSLog(@"person encodeWithCoder"); _age=[aDecoder decodeIntegerForKey:@"age"]; _name=[aDecoder decodeObjectForKey:@"name"]; _photo=[aDecoder decodeObjectForKey:@"photo"]; return self; @end 在Student类中继承Person类 增加了一个学号属性
// Created by City--Online on 15/4/22. // Copyright (c) 2015年 CYW. All rights reserved. #import "Student.h" @implementation Student - (void)encodeWithCoder:(NSCoder *)aCoder [super encodeWithCoder:aCoder]; NSLog(@"student encodeWithCoder"); [aCoder encodeObject:self.stuId forKey:@"stuId"]; - (id)initWithCoder:(NSCoder *)aDecoder if (self=[super initWithCoder:aDecoder]) { NSLog(@"student initWithCoder"); _stuId=[aDecoder decodeObjectForKey:@"stuId"]; return self; @end
自定义模型对象归档解档运行结果如下:
主要是实现NSCoding协议
储留香:天庭需要怎样的数据存储系统 在中国神话故事中,高达九重的天庭之中神仙众多,二十八星宿、三十六天罡、七十二地煞等等,每位大仙也多是长生不死,少则几百、多则几万岁,要保障如此庞大的系统组织高效运转就需要一个良好的数据存储系统。
2、设置归档存放路径(只可以设置其一,不可以并行) a) log_archive_dest:指定归档文件存放的路径,该路径只能是本地磁盘,默认为 ,不能与以下参数同使用。
社会主义 从.Net到iOS,在撸的道路上越走越远,工作之余经营着博客园http://www.cnblogs.com/5ishare,欢迎小伙伴(妹子更好)一起交流,谈谈人生理想。作为经常从网上索取免费资料的一员,要有回报回报的思想,也让更多的人少走弯路.
相关文章
- [web 前端] web本地存储(localStorage、sessionStorage)
- HTC 328T 提示手机存储不足 out of space怎么办
- 201604深圳云栖大会Workshop - 使用表格存储开发用户弹幕功能
- SAE学习-使用SAE的Storage服务存储图片
- 使用泛型存储不同类型的数据
- Java -- JDBC 学习--调用函数&存储过程
- docker存储结构解析
- FEDERATED存储引擎的使用
- 【阿里巴巴大数据实践笔记】第14章:存储和成本管理
- Android 数据存储(XML解析)
- SDCard存储
- [NHibernate]存储过程的使用(二)
- docker配置数据默认存储路径:--graph已过时,新版本中使用--data-root代替
- MySQL的数据存储方式的概述
- HBase的列族式存储介绍
- Atitit index threod type 索引原理与类型 种类 目录 1.1. 每个文档在经过底层的存储引擎持久化后1 2. MongoDB索引类型2 2.1. 包括单字段索引、复合索引
- Atitit it计算机应用体系图 大数据 爬虫 非结构数据 nosql redis mongodb 分布式存储 es搜索 可视化 多媒体与office 19.1. 14.3 计
- Atitit.数据索引 的种类以及原理实现机制 索引常用的存储结构 v3 r819
- Atitit 数据存储的数据表连接attilax总结
- 【华为云技术分享】云小课 | OBS提供多方面数据安全保障,让存储放心、贴心、省心
- python-爬虫,实现输入关键字,然后爬取关键字主页代码并存储到mysql数据库
- 大数据存储引擎 NoSQL极简教程 An Introduction to Big Data: NoSQL
- 【云原生 | 19】Docker数据存储详解
- lotus 存储数据 2k 本地测试网
- 定义存储过程如下:
- “位图(BitMaps):Redis中高效存储布尔类型数据的利器”
- filebeat将收集的日志存储在指定es索引库并在kibana上展示日志数据(三)
- 【维生素C语言】第九章 - 数据的存储