数据存储值归档Archive
2024-08-27 13:46:24
先比較一下各个数据存储之间的关系:
关于归档。是ios中的shu'j数据存储中的一种数据存储方式。以下了解一下归档中的一个实例:
以下的是父类person
#import <Foundation/Foundation.h> @interface Person : NSObject <NSCoding>
@property (nonatomic,copy) NSString *name;
@property (nonatomic,assign) int age;
@property (nonatomic,assign ) BOOL sex;
@property (nonatomic,assign) float height;
@property (nonatomic,assign) double weight;
@end #import "Person.h" @implementation Person //有存必然是有取。所以存是为了取
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super init];
if (self) {
NSLog(@"存在");
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeInt32ForKey:@"age"];
self.sex = [aDecoder decodeBoolForKey:@"sex"];
self.height = [aDecoder decodeFloatForKey:@"height"];
self.weight = [aDecoder decodeDoubleForKey:@"weight"];
}
return self;
} -(void)encodeWithCoder:(NSCoder *)aCoder{ //有文件就能够看出编码的是一个方式编码
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInt:self.age forKey:@"age"];
[aCoder encodeBool:self.sex forKey:@"sex"];
[aCoder encodeFloat:self.height forKey:@"height"];
[aCoder encodeDouble:self.weight forKey:@"weight"]; }
@end
子类student继承person类: #import <Foundation/Foundation.h>
#include "Person.h" @interface Student : Person @property (nonatomic,copy )NSString * content;
@property (nonatomic,assign ) float grade; @end
#import "Student.h" @implementation Student //有存必然是有取,所以存是为了取
- (id)initWithCoder:(NSCoder *)aDecoder{ if (self = [super initWithCoder:aDecoder]) {
NSLog(@"存在");
self.content = [aDecoder decodeObjectForKey:@"content"];
self.grade = [aDecoder decodeFloatForKey:@"grade"];
}
return self;
} -(void)encodeWithCoder:(NSCoder *)aCoder{ [super encodeWithCoder:aCoder];
//有文件就能够看出编码的是一个方式编码
[aCoder encodeObject:self.content forKey:@"content"];
[aCoder encodeFloat:self.grade forKey:@"grade"]; } @end
以下是在ViewController中中对数据通过两button对数据进归档和解归档
#import "ViewController.h"
#import "Person.h"
#import "Student.h" @interface ViewController ()
//分别室保存和获取的两个button
- (IBAction)saveArchive:(id)sender;
- (IBAction)obtainUnarchive:(id)sender;
@end @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
} //保存数据
- (IBAction)saveArchive:(id)sender { NSLog(@"開始编码归档而且保存");
//归档需要要素:1、保存对象 2、保存的文件文件夹 3、保存管理器(归档器) //获取要保存的对象,对象必需要幼稚等等
// Person *person = [[Person alloc]init];
// person.age = 18;
// person.name = @"linyu";
// person.sex = YES; //表示为男的
// person.height = 180;
// person.weight = 60; Student *stu = [[Student alloc]init];
stu.age = 18;
stu.name = @"linyu";
stu.sex = YES;
stu.height = 180;
stu.weight = 60;
stu.content = @"I am a student !";
stu.grade = 98.9; //获取要保存文件的路径
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString * dir = [documentDir stringByAppendingString:@"doc.txt"];
NSLog(@"%@",dir);
[NSKeyedArchiver archiveRootObject:stu toFile:dir];
} //获取数据
- (IBAction)obtainUnarchive:(id)sender {
//获取归档之后的内容、
NSLog(@"获取内容");
NSString *documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString * dir = [documentDir stringByAppendingString:@"doc.txt"];
Student *per = [NSKeyedUnarchiver unarchiveObjectWithFile:dir];
NSLog(@"%@",dir);
NSLog(@"%@ %d %d %.2f %.2f %.2f %@",per.name,per.age,per.sex,per.height,per.weight,per.grade,per.content);
}
@end
输出的结果:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
总结:
我们能够知道文件归档:
1、编号设置(preferrence)NSUserDefault:这个事实上也是通过plist文件来存储的。仅仅只是时里面已经通过封装了以后台上面了。存储在preferrence文件里。
2、plist文件。通过自己获取文件的路径(而且创建)。将数据存储到里面。这里一般都是在dorectory这个文件夹以下。
3、归档:归档的数据存储是经过一定的压缩。所以显示的不是明文的存储方式,而且归档是用来存储对象的。
4、注意要存储的类中遵循NSCoding 协议。
四种存储数据的方式中上面的两种方式是仅仅能够存储对应的主要的数据类型。——> 产生归档的方式进行存储。
(能够存储对象)
假设对象是NSString、NSDictionary、NSArray、NSData、NSNumber等类型,能够直接用NSKeyedArchiver进行归档和恢复
不是全部的对象都能够直接用这样的方法进行归档,仅仅有遵守了NSCoding协议的对象才干够
NSCoding协议有2个方法:
encodeWithCoder:
每次归档对象时,都会调用这种方法。一般在这种方法里面指定怎样归档对象中的每一个实例变量,能够使用encodeObject:forKey:方法归档实例变量
initWithCoder:
每次从文件里恢复(解码)对象时,都会调用这种方法。一般在这种方法里面指定怎样解码文件里的数据为对象的实例变量,能够使用decodeObject:forKey方法解码实例变量
最新文章
- MariaDB学习
- 浅谈 原生javaScript&;&;react 实现全局触摸按钮(附带对addeventlistener的了解)
- PGA
- Oracle 物化视图 说明
- window IIS6/IIS7取消脚本执行权限,禁止运行脚本木马
- C#系统缓存全解析
- SQL遍历字符串的方法
- 在线演示红黑树(javascript)
- 关于ios11 tableView和ScrollView受导航栏影响向下偏移的问题
- Redis的那些最常见面试问题
- react &; youtube
- android 开发 View _16 自定义计步器View、自定义柱状图View
- linux查找某段时间修改的文件的总大小
- 进程表示之进程ID号
- Codeforces 934D - A Determined Cleanup
- C++ 中的continue理解
- 软件测试----H模型
- python的redis简单使用
- treap Python实现
- SpringBoot中 application.yml /application.properties常用配置介绍
热门文章
- 从终端运行python程序
- ECNUOJ 2616 游黄山
- Android Studio更改项目SDK的版本
- [Python] Accessing Array Elements
- An internal error occurred during: &;quot;Checking tomcat state&;quot;. Error while reading server.xml
- oracle之ROWNUM的查询应用
- Hadoop2.2集群安装配置-Spark集群安装部署
- BZOJ5204: [CodePlus 2018 3 月赛]投票统计
- 冒泡排序算法 C#版
- HtmlHelper的扩展分页方法