技术标签: iOS
普通数组的归档和解档
普通数组的归档流程:
NSKeyedArchiver
类的 NSKeyedArchiver+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path
方法将数据归档//普通数组归档
- (IBAction)onClickBtn1:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"numbers.plist"];
NSArray *numbers = @[@"one",@"two"];
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:numbers toFile:path];
if (success) {
NSLog(@"文件归档成功");
}
}
注:
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path
方法有个BOOL类型的返回值。说明归档操作是有可能失败的。一般来说,归档失败最多的情况有两种:文件路径不存在以及无写入权限。后面会单独讲。
普通数组解档流程:
NSKeyedUnarchiver
类的+ (nullable id)unarchiveObjectWithFile:(NSString *)path;
方法进行解档,读取内容。//普通数组解档
- (IBAction)onClickBtn2:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"numbers.plist"];
//将path文件路径的数据解档出来
NSArray *numbers = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"numbers=%@",numbers);
}
因为文件夹不存在造成归档失败
归档是有可能失败。
很多时候造成归档失败的原因是,你给出了一个文件路径,但是这个文件的文件夹路径并不存在。所以程序无法给你归档,创建文件。这时,我们应该要先创建文件夹,再进行归档。
上面我们的例子我们取的文件夹路径是沙盒里面的Documents文件夹。我们是让程序在这个文件加里面创建了一个文件进行归档。
假如我们想要在Documents文件夹内的Archive文件夹进行归档呢?会发生什么情况?
//因为文件夹不存在造成归档失败
- (IBAction)onClickBtn1_2:(id)sender {
//沙盒home目录
NSString *docPath = NSHomeDirectory();
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"Documents/Archive/numbers.plist"];
NSArray *numbers = @[@"one",@"two"];
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:numbers toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
//将path文件路径的数据解档出来
NSArray *unarchiveNumbers = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"unarchiveNumbers=%@",unarchiveNumbers);
}
结果:
2018-05-10 15:42:54.966 iOSArchiveAndUnArchive[11508:441222] 沙盒路径:/Users/mac/Library/Developer/CoreSimulator/Devices/662CE852-A601-496B-827B-44A8122A5F52/data/Containers/Data/Application/EAF9F153-8F41-4650-A58E-CFC2BED411EE
2018-05-10 15:42:57.618 iOSArchiveAndUnArchive[11508:441222] 归档失败
2018-05-10 15:42:57.619 iOSArchiveAndUnArchive[11508:441222] unarchiveNumbers=(null)
以上代码会导致归档失败。看了看,原来Documents目录下并没有Archive文件夹。
那么,在归档之前,我们最好先判断下文件夹是否存在,如果不存在,则先创建文件夹,然后再进行归档操作。
//先判断下文件夹是否存在,如果不存在,则先创建文件夹,然后再进行归档操作
- (IBAction)onClickBtn1_2:(id)sender {
//沙盒home目录
NSString *docPath = NSHomeDirectory();
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"Documents/Archive/numbers.plist"];
//获取文件夹路径
NSString *directory = [path stringByDeletingLastPathComponent];
//判断文件夹是否存在
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:directory isDirectory:nil];
//如果不存在则创建文件夹
if (!fileExists) {
NSLog(@"文件夹不存在");
//创建文件夹
NSError *error = nil;
[[NSFileManager defaultManager] createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"error=%@",error.description);
} else {
NSLog(@"文件夹创建成功");
}
}
NSArray *numbers = @[@"one",@"two"];
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:numbers toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
//将path文件路径的数据解档出来
NSArray *unarchiveNumbers = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"unarchiveNumbers=%@",unarchiveNumbers);
}
因为无写权限造成归档失败
导致归档失败的另一个原因是程序没有写权限。
因为归档操作本质是创建一个文件,然后在这个文件里面存储数据。如果你在一个文件夹路径上面并没有写权限,那么失败也就能理解了。
有些不熟悉的人可能会遇到这个问题:我在模拟器上归档操作是成功的,可是在真机上运行的时候却归档失败了。到底是什么原因?
这个就是遇到了写权限的问题了。在模拟器上,因为是写在电脑上面的路径的,所以这个路径我们有写权限。但是真机不一样,真机上我们一般情况下只能操作沙盒路径里面文件夹。无法在其他的地方写文件。
//因为无写权限造成归档失败
- (IBAction)onClickBtn1_3:(id)sender {
//安装目录
//NSString *docPath = [[NSBundle mainBundle] bundlePath];
//沙盒home目录
NSString *docPath = NSHomeDirectory();
NSLog(@"docPath=%@",docPath);
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"Documents/numbers.plist"];
//获取文件夹路径
NSString *directory = [path stringByDeletingLastPathComponent];
if (![[NSFileManager defaultManager] isWritableFileAtPath:directory]) {
NSLog(@"目录无写入权限");
}
NSArray *numbers = @[@"one",@"two"];
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:numbers toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
//将path文件路径的数据解档出来
NSArray *unarchiveNumbers = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"unarchiveNumbers=%@",unarchiveNumbers);
}
一般因为没有权限导致归档失败,都是在两个目录:
- 安装包目录
[[NSBundle mainBundle] bundlePath]
。这个很明显。我们一般只操作沙盒目录,对安装包目录是没有权限的(模拟器不算)。- 沙盒home目录的根目录。这个比较容易出错了。我们明明可以操作沙盒的,为什么没有权限呢?
因为你是在沙盒的根目录操作。NSHomeDirectory
这个就是沙盒的根目录。直接在这个目录下归档也是会导致归档失败的。一般我们都是在沙盒的Documents目录里面玩。
普通字典的归档和解档
普通字典归档跟上面数组的归档的操作是一样的。
//普通字典归档
- (IBAction)onClickBtn3:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"personDict.archive"];
NSDictionary *personDict = @{
@"name":@"shixueqian",
@"age":@(27)
};
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:personDict toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
}
这里的后缀名用了
.archive
。实际上对于归档操作来说,用什么后缀名都无所谓的。只是我们一般习惯用.archive
。之前数组的时候用.plis
t,是为了方便演示。
普通字典解档跟上面数组的解档的操作是一样的。
//普通字典解档
- (IBAction)onClickBtn4:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"personDict.archive"];
//将path文件路径的数据解档出来
NSDictionary *personDict = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"personDict=%@",personDict);
}
多个普通对象同时归档和解档
上面的例子是将单个的数组或者单个字典归档到一个文件。其实我们也可以将多个数组、字典、字符串、数组等内容归档到同一个文件里面去。
//多个普通对象同时归档
- (IBAction)onClickBtn5:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"manyData.plist"];
NSInteger age = 27;
NSString *name = @"shixueqian";
NSArray *array = @[@"one",@"two"];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:name forKey:@"name"];
[archiver encodeInteger:age forKey:@"age"];
[archiver encodeObject:array forKey:@"numbers"];
//完成归档
[archiver finishEncoding];
//将归档好的数据写到文件里面
[data writeToFile:path atomically:YES];
}
//多个普通对象同时解档
- (IBAction)onClickBtn6:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"manyData.plist"];
NSMutableData *data = [NSMutableData dataWithContentsOfFile:path];
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
NSString *name = [unarchiver decodeObjectForKey:@"name"];
NSInteger age = [unarchiver decodeIntegerForKey:@"age"];
NSArray *numbers = [unarchiver decodeObjectForKey:@"numbers"];
[unarchiver finishDecoding];
NSLog(@"name=%@,age=%zd,numbers=%@",name,age,numbers);
}
自定义对象的归档和解档
上面的例子,归档的内容都是系统Foundation框架提供的类以及一些基本的数据类型。
Foundation框架提供的类都是实现了NSCoding
协议的,所以能够直接进行归档。
如果我们自己自定义了一个类,例如Person类,是无法能够直接将这个类进行归档操作的。
如果想要对自定义的类创建出来的对象进行归档,我们需要先实现NSCoding
协议。
Person.h文件内容:
#import <UIKit/UIKit.h>
@interface Person : NSObject<NSCoding>
@property (nonatomic,copy)NSString *name;
@property (nonatomic,assign)NSInteger age;
//有些属性可以不进行归档
@property (nonatomic,assign)CGFloat height;
@end
Person.m文件内容:
#import "Person.h"
@implementation Person
//NSCoding协议方法:将需要归档的属性进行归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
//NSCoding协议方法:将需要解档的属性进行解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
//重写description方法,方便直接打印对象
- (NSString *)description {
return [NSString stringWithFormat:@"person.name=%@,person.age=%zd,person.height=%f",self.name,self.age,self.height];
}
@end
Person类实现了NSCoding
协议之后,就可以跟普通的Foundation框架提供的基本对象一样进行归档和解档操作了。
//自定义对象归档
- (IBAction)onClickBtn7:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"person.archive"];
Person *person = [[Person alloc] init];
person.name = @"谦言忘语";
person.age = 27;
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:person toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
}
//自定义对象解档
- (IBAction)onClickBtn8:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"person.archive"];
Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"person=%@",person);
}
自定义对象数组的归档和解档
通过上面的内容,普通数组NSArray对象的归档和解档已经知道怎么做了。自定义对象的归档和解档操作也已经知道怎么做了。
那么,自定义对象数组的归档和解档怎么处理呢?比如自定义了一个Person类,然后有一个数组,里面都是person类的对象。NSArray *array = @[person1,person2,person3];
。
嗯,其实挺简单的。当我们自定义的类实现了NSCoding
协议之后,存放自定义类对象的数组就可以当做是普通数组一样进行归档和解档了。
下面代码中的Person类已经实现了NSCoding
协议,代码上面有,就省略了。
//自定义对象数组归档
- (IBAction)onClickBtn9:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"persons.archive"];
Person *person1 = [[Person alloc] init];
person1.name = @"大帅哥";
person1.age = 18;
Person *person2 = [[Person alloc] init];
person2.name = @"谦言忘语";
person2.age = 27;
NSArray *persons = @[person1,person2];
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:persons toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
}
//自定义对象数组解档
- (IBAction)onClickBtn10:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"persons.archive"];
NSArray *persons = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"persons=%@",persons);
}
自定义对象里面有另一个自定义对象的归档和解档
上面的Person类,里面的属性的类型就只有Foundation框架里面提供的基本对象以及基本的数据类型。比如NSString
类型,NSInterger
类型等等。
那假如我们自定义的类里面有另外一个自定义的类的对象作为属性呢?
其实本篇归档和解档的问题可以遵循一个原则:
只要一个类实现了NSCoding
协议,那么这个类的对象就可以进行归档和解档。类里面的属性实现了NSCoding
协议,那么这个属性就可以进行归档和解档。
比如上面的Person类,实现了NSCoding
协议,那么这个类就可以进行归档和解档。
类的属性name
类型是NSString
,已经实现过了NSCoding
协议,所以可以将这个属性归档。age
是基本数组类型,也可以进行归档。
但是如果这个Person类中还有一个属性为pet
,类型是我们自定义的Pet
类,并且这个类没有实现NSCoding
协议,那么这个pet的属性就无法进行归档。但是,只要Pet类实现了NSCoding
协议,那就可以了。
以下代码中,自定义的Pet类里面拥有一个master属性,类型为Person。
Pet.h文件代码:
#import <Foundation/Foundation.h>
#import "Person.h"
@interface Pet : NSObject<NSCoding>
@property (nonatomic,strong)NSString *name;
@property (nonatomic,strong)Person *master;
@end
Pet.m文件代码:
#import "Pet.h"
@implementation Pet
//NSCoding协议方法:将需要归档的属性进行归档
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.master forKey:@"master"];
}
//NSCoding协议方法:将需要解档的属性进行解档
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.master = [aDecoder decodeObjectForKey:@"master"];
}
return self;
}
//重写description方法,方便直接打印对象
- (NSString *)description {
return [NSString stringWithFormat:@"pet.name=%@,pet.master:%@",self.name,self.master];
}
@end
//自定义对象里面有另一个自定义对象归档
- (IBAction)onClickBtn11:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"pet.archive"];
Person *person = [[Person alloc] init];
person.name = @"谦言忘语";
person.age = 27;
Pet *pet = [[Pet alloc] init];
pet.name = @"小白";
pet.master = person;
//将数据归档到path文件路径里面
BOOL success = [NSKeyedArchiver archiveRootObject:pet toFile:path];
if (success) {
NSLog(@"归档成功");
}else {
NSLog(@"归档失败");
}
}
//自定义对象里面有另一个自定义对象解档
- (IBAction)onClickBtn12:(id)sender {
//沙盒ducument目录
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//完整的文件路径
NSString *path = [docPath stringByAppendingPathComponent:@"pet.archive"];
Pet *pet = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"pet=%@",pet);
}
文章浏览阅读3.2k次,点赞3次,收藏20次。项目微服务架构图微服务架构根据目前产品存在的问题,针对快速开发、海量用户、大量数据、低延迟等互联网应用的实际需要,通过对业务架构、系统架构、基础架构、技术架构进行设计,彻底解决系统解耦、性能低下等问题,而且支持云计算部署,可以满足高并发、高可用、高稳定。微服务并没有一个官方的定义,可以理解为一种架构风格。大数据管理数据处理过程图大数据(big data),指无法在一定时间范围内用常规软件工具进行捕捉、管理和处理的数据集合,是需要新处理模式才能具有更强的决策力、洞察力。大数据处理的主要流._微服务化开发流程图
文章浏览阅读1.5k次。前言前几天配置了一下centos系统的邮件发送。下面总结的是linux配置外部邮箱发送邮件的操作。(以centos系统为例,主要是注意配置文件内容即可)centos邮件发送配置由于配置的外部邮箱发送邮件,依赖第三方的邮件服务器发送邮件,需要第三方的邮箱的账号,密码,客户端授权等。配置简易,通常用于监控或其他通知邮件,不受云服务器对某些邮件端口的限制(例如阿里云的25),需注意于第..._linux设置不用本地邮箱发邮件
文章浏览阅读3k次,点赞6次,收藏46次。ubuntu16.04源码安装autoware.ai 1.13.0官网提供的autoware版本与ubuntu系统适配情况,作者使用ubuntu16.04,选择版本v1.13.0下图为官方适配表详见https://github.com/Autoware-AI/autoware.ai/wiki/Source-Build,本安装步骤按照官网步骤进行,期间遇到的一些坑也会指出作者已经安装完成ROS、QT5、CUDA9.0,未安装的请参考其他博客,在此不再赘述ROShttps://blog.csdn.ne_autoware1.13.0中qpoases文件
文章浏览阅读3.8w次,点赞60次,收藏309次。1. 图片加载、灰度图、 显示和保存# Created by 牧野 CSDNfrom PIL import Imageimg = Image.open('01.jpg')imgGrey = img.convert('L')img.show()imgGrey.show()img.save('img_copy.jpg')imgGrey.save('img_gray.jpg')..._image.bicubic
文章浏览阅读6.4k次,点赞6次,收藏19次。原文链接:使用Vue开发Chrome插件 - 愧怍的小站 (kuizuo.cn)前言我当时学习开发Chrome插件的时候,还不会Vue,更别说Webpack了,所以使用的都是原生的html开发,效率就不提了,而这次就准备使用vue-cli来进行编写一个某B站获取视频信息,评论的功能(原本是打算做自动回复的),顺便巩固下chrome开发(快一年没碰脚本类相关技术了),顺便写套模板供自己后续编写Chrome插件做铺垫。关于Chrome插件开发的基本知识就不赘述了,之前写过一篇原生开发的Chrome._vite+vue开发谷歌插件
文章浏览阅读5.4k次,点赞3次,收藏21次。第1章 微程序控制器微程序控制器是一种控制器,同组合逻辑控制器相比较,具有规整性、灵活性、可维护性等一系列优点,因而在计算机设计中逐渐取代了早期采用的组合逻辑控制器,并已被广泛地应用。在计算机系统中,微程序设计技术是利用软件方法来设计硬件的一门技术 。中文名 微程序控制器外文名 Microprogram controller基本思想 按照通常的解题程序的方法优 点 规整性、灵活性、可维护性设计技术 利用软件方法来设计硬件定 义 采用微程序控制方式的控制器1.1_时空无限 csdn
文章浏览阅读3.7k次,点赞11次,收藏71次。UDS刷写_uds刷写
文章浏览阅读1.6k次。S7-1200 与V20 的USS 通讯,S7-1200 PLC要求加CM1241 RS485通信模块,通过USS协议库指令编程。USS协议库指令集成在编程软件中。S7-1200和V20通讯实例请参考以下文档与视频:1. 1,通信连接 V20变频器通过RS485线缆与PLC连接,使用标准的MODBUS通信协议进行通讯,通过modbus通讯,PLC给V20变频器发送指令可对变频器进行启停调频的操作。..._1200跟v20通讯详细步骤
文章浏览阅读127次。委外PR的BOM清单导出_bom在pr
文章浏览阅读1w次,点赞10次,收藏20次。个人简历 个人简历 个人资料 姓 名: 婚姻状况: 照片 出 生: 政治面貌: _html table demo
文章浏览阅读1.6w次,点赞3次,收藏12次。麦克风灵敏度,通常都是以负数形式呈现,比如-45db、-47db。麦克风灵敏度为何是负数呢?db是一个无量纲单位,因此就有一个参考系。麦克风0db的定义是:在1pa声压下,麦克风输出1V电压时为0db。计算公式如下:Lm=20lgVm/Vs,Vs=1v,Vm为麦克风在1帕时输出的电压。通常情况下,麦克风输出的电压都是mV级别,因此得到的Lm一般为负数。比如 -47db,对应的Vm值_mic灵敏度计算公式
文章浏览阅读470次。艺赛旗 RPA9.0全新首发免费下载 点击下载http://www.i-search.com.cn/index.html?from=line10 前言说到处理循环,我们习惯使用 for, while 等,比如依次打印每个列表中的字符:lis = [‘I’, ‘love’, ‘python’]for i in lis:print(i)输出:Ilovepython在打印内容字节数..._python循环读取文件的每一行 节省内存