归档和解档配合NSFile存储数据
NSString *Name = @"yc";
//第一个常量NSDocumentDirectory表示正在查找沙盒Document目录的路径(如果参数为NSCachesDirectory则表示沙盒Cache目录),
//第二个常量NSUserDomainMask表明我们希望将搜索限制在应用的沙盒内;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathDirectory = [paths lastObject];
NSLog(@"Documents目录路径=%@",pathDirectory);
//创建文件stringByAppendingPathComponent:路径拼接
NSString *filePath = [pathDirectory stringByAppendingPathComponent:@"wyc"];
NSLog(@"filePath===%@",filePath);
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]){
}else{
NSError *error ;
BOOL isSuccess = [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
if (isSuccess) {
NSLog(@"创建文件夹成功");
}else{
NSLog(@"创建文件夹失败");
}
}
//深一层文件路径
NSString* fileDirectory = [filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.arc",Name]];
NSLog(@"new === %@",fileDirectory);
//解档
Person *man = [[Person alloc]init];
man.name = @"大傻";
man.age = @"18";
BOOL success = [NSKeyedArchiver archiveRootObject:man toFile:fileDirectory];
if (success){
NSLog(@"归档成功");
}else{
NSLog(@"归档失败");
}
id getFile = [NSKeyedUnarchiver unarchiveObjectWithFile:fileDirectory];
NSLog(@"%@",getFile);
//移除文件
-(BOOL)removeFile:(NSString *)fileName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0]stringByAppendingPathComponent:@"wyc"];
NSFileManager *manager = [NSFileManager defaultManager];
if (![manager fileExistsAtPath:path]){
return YES;
}
NSString* fileDirectory = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.arc",fileName]];
BOOL success = [manager removeItemAtPath:fileDirectory error:nil];
if (success){
return YES;
}
else{
return NO;
}
}
#import "BaseModel.h"
#import <objc/runtime.h>
@implementation BaseModel
#pragma mark 数据持久化
//序列化
- (void)encodeWithCoder:(NSCoder *)aCoder{
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (i = 0; i < outCount; i++){
objc_property_t property = properties[i];
const char* char_f = property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
id propertyValue = [self valueForKey:(NSString *)propertyName];
if (propertyValue){
[aCoder encodeObject:propertyValue forKey:propertyName];
}
}
}
//反序列化
- (id)initWithCoder:(NSCoder *)aCoder{
self = [super init];
if (self){
unsigned int outCount, i;
objc_property_t *properties =class_copyPropertyList([self class], &outCount);
for (i = 0; i<outCount; i++){
objc_property_t property = properties[i];
const char* char_f = property_getName(property);
NSString *propertyName = [NSString stringWithUTF8String:char_f];
NSString *capital = [[propertyName substringToIndex:1] uppercaseString];
NSString *setterSelStr = [NSString stringWithFormat:@"set%@%@:",capital,[propertyName substringFromIndex:1]];
SEL sel = NSSelectorFromString(setterSelStr);
[self performSelectorOnMainThread:sel
withObject:[aCoder decodeObjectForKey:propertyName]
waitUntilDone:[NSThread isMainThread]];
}
}
return self;
}
归档和解档配合NSFile存储数据的更多相关文章
- 【IOS学习基础】归档和解档
一.归档介绍 1.归档是指用某种格式来保存一个或多个对象,以便以后还原这些对象的过程.归档是将数据持久化的一种方式(所谓数据持久化,就是指在IOS开发过程中,将数据保存到本地,能够让程序的运行更加流畅 ...
- OC 归档和解档
#import <Foundation/Foundation.h> #define PATH @"/Users/mac/Desktop/file.txt" int ma ...
- 利用Runtime对Ivar实例变量进行共用的归档和解档方式
一.介绍 在OC中每一个对象持有的变量都是实例变量,实例变量包括成员变量和属性变量,在runtime中用Ivar表示对象的实例变量.其实,runtime源码中可以看到,Ivar也是一个结构体(基本上在 ...
- runtime之归档和解档
IOS开发之NSCoding协议(使用runtime)近期学习IOS的runtime库,然后看到之前写的NSCoding协议有点复杂,如果属性少还好,如果100多个属性,则会显得麻烦.下面使用常规方式 ...
- iOS基础知识之归档和解档
归档和解档:即将数据写入文件和从文件中读取数据. 此处以plist文件为例说明, 一.plist文件使用时的注意事项: 1.plist文件中仅支持写入Array,Dictionary,Boolean, ...
- 数据存储之归档解档 NSKeyedArchiver NSKeyedUnarchiver
在构建应用程序时,有一个重要的问题是如何在每次启动之间持久化数据,以便重现最后一次关闭应用前的状态.在iOS和OS X上,苹果提供了三种选择:Core Data.属性列表(Property List) ...
- iOS 数据存储 - 归档和解归档
这里的归档主要是用于自定义类的归档和解档.我们这里使用NSKeyedArchiver和NSKeyedUnarchiver来归档和解档. 注意:自己定义的类需要实现<NSCoding>,如: ...
- iOS开发中的4种数据持久化方式【一、属性列表与归档解档】
iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...
- iOS--归档和解档(Archiver)、(UnArchiver)
一.已有类型的归档和解档 首先来看一个简单的例子: //第一方式:归档对象 //对象-->文件 NSArray *array = [NSArray arrayWithObjects:@" ...
随机推荐
- git分布式版本控制系统权威指南学习笔记(五):git checkout
文章目录 分离头指针 通过cat可以查看当前的分支 通过branch查看当前分支 checkout commitId(真正的
- sublime 文件无法拖放
sublime 文件无法拖放解决方法 https://github.com/emetio/pyv8-binaries 解决: 1. 去gihub下载 PyV8,连接地址:https://github. ...
- python3 可变数据类型和不可变数据类型
python内置有6种对象类型: Number 数值型 int 整型 不可变 float 浮点型 不可变 complex 复数 不可变 String 字符串 不可变 Tuple 元组 不可变 ...
- java读取pdf文本转换html
补充:一下代码基于maven,现将依赖的jar包单独导出 地址:pdf jar 完整代码地址 也就两个文件 java读取pdf中的纯文字,这里使用的是pdfbox工具包 maven引入如下配置 < ...
- centos7.4 系统优化
static 表示该服务与其他服务相关联,不能单独设置该服务的启动状态 disabled 表示禁止开机启动 enabled 表示允许开机启动 auditd.service enabled autovt ...
- Ansible随笔8
自定义模块的开发模式 1.决定自定义模块的存放路径 编辑/etc/ansible/ansible.cfg文件,修改library = /usr/share/ansible/. 这样就告诉ansible ...
- 深入vue
- 笔记41 Spring Web Flow——Demo
订购披萨的应用整体比较比较复杂,现拿出其中一个简化版的流程:即用户访问首页,然后输入电话号(假定未注册)后跳转到注册页面,注册完成后跳转到配送区域检查页面,最后再跳转回首页.通过这个简单的Demo用来 ...
- Linux下安装.NETCore3.0
今天把.NETCore从2.2升到3.0记录一下 Download .NET Core 3.0 :https://dotnet.microsoft.com/download/dotnet-core/ ...
- Java用集合实现斗地主发牌
本文以java双列集合HashMap为核心实现发牌操作: 思路: 1.准备牌:创建一个Map集合,存储牌的索引和组装好的牌 创建一个lis ...