当前位置:网站首页>OC-归档(序列化)(了解的不多 没细看)
OC-归档(序列化)(了解的不多 没细看)
2022-08-04 21:37:00 【彭同学她同桌】
IOS的本地存储:plist属性列表(Property List),归档,sqlite,coredata
什么是归档
归档就是将对象打包成二进制序列
归档的作用
归档可以将程序中使用的多个对象及其属性值,以及他们的相互关系保存到文件中 或者发送给另一个进程
简单点说就是将对象以对象的形式存储在文件中 并且还可以以对象的形式从文件中拿出来
够可以将相互关联的多个对象归档为二进制文件,
NSCoding协议
归档需要先引入<NSCoding>
协议
@protovol NSCoding
-(void)encodeWithCoder:(NSCoder*)aCoder;
-(id)initWithCoder:(NSCoder*)aDecoder;
@end
NSCoder 是一个抽象类 NSKeyedArchiver和NSKeyedUnarchiver是NSCoder的子类
NSCoder就相当于一个二进制空间
NSCoder的实例成为归档器
-(void)encodeWithCoder:(NSCoder*)aCode//指定对象归档规则
-(void)encodeWithCoder:(NSCoder*)aCode
{
//如果父类适用于NSCoding协议 则super也需要调用
//[super encoderWithCoder:aCoder];
[coder encodeObject:/*归档变量*/ forKey:/*归档名称*/];
[coder encodeObject:self.name forKey:@"name"];
}
-(id)initWithCoder:(NSCoder*)aDecoder//指定对象解档规则
//如果父类适用NSCoding协议 则
//self = [super initWithCoder:aDecoder];
//如果父类不适用则调用
//self = [super init];
接收变量 = [coder decodeObjectForKey:/*归档名称*/];
self.name = [coder decodeObjectForKey:@"name"];
NSKeyedArchiver archiveRootObject:toFile: //对目标对象进行归档
[NSKeyedArchiver archiveRootObject:归档对象 toFile:归档目标路径]
[NSKeyedArchiver archiveRootObject:a toFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"]
NSKeyedUnarchiver unarchiveObjectWithFile://通过解档目标文件初始化对象
A *a = [NSKeyedUnarchiver unarchiveObjectWithFile:目标文件路径];
A *a = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"];
(id) initForWritingwithMutableData:(NSMutableData*)data
多对象的
将预先生成的NSMuable实例作为参数 初始化NSkeyedArchiver实例 这个data的数据会被保留 最后作为归档
NSMutableData *data = [NSMutableData data];//创建一个data对象
//根据二进制流创建NSKeyedArchiver对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//感觉可能是将在archiver和data之间创建一个二进制流
[archiver encodeObject:a forKey:@“a”];
[archiver encodeObject:aa forKey:@“aa”];
实例
dog.h
@interface Dog : NSObject<NSCoding>
@property (nonatomic,copy)NSString* name;
@end
dog.m
#import "dog.h"
@implementation Dog
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.name forKey:@"name"];
}
-(nullable instancetype)initWithCoder:(NSCoder *)coder
{
if(self = [super init])
{
self.name = [coder decodeObjectForKey:@"name"];
}
return self;
}
@end
main.m
//
// main.m
// OCTest
//
// Created by Renkai Yang on 2022/7/26.
//
#import <Foundation/Foundation.h>
#import "dog.h"
@interface A:NSObject<NSCoding>
@property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)int age;
@property (nonatomic,assign)double weight;
@property (nonatomic,strong)Dog*dog;
@end
@implementation A
-(void)encodeWithCoder:(NSCoder *)coder//设定归档规则
{
[coder encodeObject:self.name forKey:@"name"];
[coder encodeInt:self.age forKey:@"age"];
[coder encodeDouble:self.weight forKey:@"weight"];
[coder encodeObject:self.dog forKey:@"dog"];//如果要给自定义类归档解档 就需要在自定义类中制定归档解档规则
}
-(nullable instancetype)initWithCoder:(NSCoder *)coder//设定解挡规则
{
if(self = [super init])//这里要初始化主要是用来给新对象初始化
{
self.name = [coder decodeObjectForKey:@"name"];
self.age = [coder decodeIntForKey:@"age"];
self.weight = [coder decodeDoubleForKey:@"weight"];
self.dog = [coder decodeObjectForKey:@"dog"];
}
return self;
}
@end
void test()//进行归档操作
{
A *a = [[A alloc]init];
a.name = @"Yang";
a.age = 23;
a.weight = 58.0;
a.dog = [[Dog alloc]init];
a.dog.name = @"大黄";
NSLog(@"归档开始");
if([NSKeyedArchiver archiveRootObject:a toFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"])//将a对象归档到指定文件中
{
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
test();
A *a = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"];//通过解档文件对a进行初始化
NSLog(@"%@",a.name);
NSLog(@"%d",a.age);
NSLog(@"%f",a.weight);
NSLog(@"%@",a.dog.name);
}
return 0;
}
多对象归档
dog.h
@interface Dog : NSObject<NSCoding>
@property (nonatomic,copy)NSString* name;
@end
dog.m
#import "dog.h"
@implementation Dog
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:self.name forKey:@"name"];
}
-(nullable instancetype)initWithCoder:(NSCoder *)coder
{
if(self = [super init])
{
self.name = [coder decodeObjectForKey:@"name"];
}
return self;
}
@end
main.m
//
// main.m
// OCTest
//
// Created by Renkai Yang on 2022/7/26.
//
#import <Foundation/Foundation.h>
#import "dog.h"
@interface A:NSObject<NSCoding>
@property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)int age;
@property (nonatomic,assign)double weight;
@property (nonatomic,strong)Dog*dog;
@end
@implementation A
-(void)encodeWithCoder:(NSCoder *)coder//设定归档规则
{
[coder encodeObject:self.name forKey:@"name"];
[coder encodeInt:self.age forKey:@"age"];
[coder encodeDouble:self.weight forKey:@"weight"];
[coder encodeObject:self.dog forKey:@"dog"];//如果要给自定义类归档解档 就需要在自定义类中制定归档解档规则
}
-(nullable instancetype)initWithCoder:(NSCoder *)coder//设定解挡规则
{
if(self = [super init])//这里要初始化主要是用来给新对象初始化
{
self.name = [coder decodeObjectForKey:@"name"];
self.age = [coder decodeIntForKey:@"age"];
self.weight = [coder decodeDoubleForKey:@"weight"];
self.dog = [coder decodeObjectForKey:@"dog"];
}
return self;
}
@end
void test()
{
A *a = [[A alloc]init];
a.name = @"a";
a.age = 23;
a.weight = 58.0;
a.dog = [[Dog alloc]init];
a.dog.name = @"大黄";
A *aa = [[A alloc]init];
aa.name = @"aa";
aa.age = 22;
aa.weight = 57.0;
aa.dog = [[Dog alloc]init];
aa.dog.name = @"大黄1";
NSLog(@"多对象归档开始");
NSMutableData *data = [NSMutableData data];
//根据二进制流创建NSKeyedArchiver对象
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//对对象进行归档
[archiver encodeObject:a forKey:@"a"];
[archiver encodeObject:aa forKey:@"aa"];
//结束归档
[archiver finishEncoding];
//将二进制流写入文件
if([data writeToFile:@"/Users/RenkaiYang/Desktop/archive/test.txt" atomically:YES])//将a对象归档到指定文件中
{
NSLog(@"归档成功");
}
else
{
NSLog(@"归档失败");
}
// NSLog(@"归档开始");
//这里是调用NSKeyedArchiver的对象方法archiveRootObject
// if([NSKeyedArchiver archiveRootObject:a toFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"])//将a对象归档到指定文件中
// {
// NSLog(@"归档成功");
// }
// else
// {
// NSLog(@"归档失败");
// }
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
test();
// //单对象解档
// A *a = [NSKeyedUnarchiver unarchiveObjectWithFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"];//通过解档文件对a进行初始化
// NSLog(@"%@",a.name);
// NSLog(@"%d",a.age);
// NSLog(@"%f",a.weight);
// NSLog(@"%@",a.dog.name);
//多对象解档
NSData* data = [NSData dataWithContentsOfFile:@"/Users/RenkaiYang/Desktop/archive/test.txt"];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
A *a1 = [unarchiver decodeObjectForKey:@"a"];
A *a2 = [unarchiver decodeObjectForKey:@"aa"];
NSLog(@"a1:%@",a1.name);
NSLog(@"a1:%d",a1.age);
NSLog(@"a1:%f",a1.weight);
NSLog(@"a1:%@",a1.dog.name);
NSLog(@"a2:%@",a2.name);
NSLog(@"a2:%d",a2.age);
NSLog(@"a2:%f",a2.weight);
NSLog(@"a2:%@",a2.dog.name);
}
return 0;
}
边栏推荐
- [larave]关于laravel使用form submit()不能获取值问题
- 搬走地下空间开发利用“绊脚石” 中地数码取得地下空间透明化技术突破
- How to solve the problem that the alarm information cannot be transmitted after EasyGBS is connected to the latest version of Hikvision camera?
- OD-Model【6】:YOLOv2
- LayaBox---TypeScript---Example
- docker 部署redis集群
- LayaBox---TypeScript---首次接触遇到的问题
- 如何一键重装win7系统?重装win7系统详细教程
- 大势所趋之下的nft拍卖,未来艺术品的新赋能
- Driving point cloud format changes bring efficiency improvement
猜你喜欢
Win11如何开启Telnet客户端?
零基础都能拿捏的七夕浪漫代码,快去表白或去制造惊喜吧
用Tesseract开发一个你自己的文字识别应用
Domestic PMP certificate of gold content how
如何为Web3.0世界启动完美的DAO
Axure9基本交互操作(一)
SPSS-System Clustering Hand Calculation Practice
In which industries is the PMP certificate useful?
LayaBox---TypeScript---Problems encountered at first contact
PowerCLi import license to vCenter 7
随机推荐
laravel whereDoesntHave
如何一键重装Win11系统 一键重装系统方法
DSPE-PEG-Aldehyde,DSPE-PEG-CHO,磷脂-聚乙二醇-醛基一种疏水18碳磷脂
docker 搭建mysql 主从复制
moke、动态图片资源打包显示
JdbcTemplate概述和测试
[21 days learning challenge - kernel notes] (2), based in the device tree
Altium Designer 19.1.18 - 画多边形铜皮挖空时,针对光标胡乱捕获的解决方法
【CC3200AI 实验教程 1】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-开发环境搭建
MySQL查询为啥慢了?
多个平台显示IP属地,必须大力推行互联网实名制
PRIMAL: Pathfinding via Reinforcement and Imitation Multi-Agent Learning 代码解析
立方度量(Cubic Metric)
数电快速入门(四)(组合逻辑电路的分析以及设计的介绍)
ini怎么使用? C#教程
数电快速入门(二)(复合逻辑运算和逻辑代数的基本定律的介绍)
AXI interface application of Zynq Fpga image processing - the use of axi_lite interface
LayaBox---TypeScript---举例
如何为Web3.0世界启动完美的DAO
1.读写点云文件