归档和解档配合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:@" ...
随机推荐
- 学习 Doug Lea 大神写的——Scalable IO in Java
学习 Doug Lea 大神写的--Scalable IO in Java 网络服务 Web services.分布式对象等等都具有相同的处理结构 Read request Decode reques ...
- JUC源码分析-其它工具类(一)ThreadLocalRandom
JUC源码分析-其它工具类(一)ThreadLocalRandom ThreadLocalRandom 是 JDK7 在 JUC 包下新增的随机数生成器,它解决了 Random 在多线程下多个线程竞争 ...
- JUC源码分析-集合篇(八)DelayQueue
JUC源码分析-集合篇(八)DelayQueue DelayQueue 是一个支持延时获取元素的无界阻塞队列.队列使用 PriorityQueue 来实现. 队列中的元素必须实现 Delayed 接口 ...
- c程序查找字符出现次数
#include <stdio.h> int main(){ char str[100],ch,M,Empty; int i, frequency = 0; fgets(str, (siz ...
- C# WinfForm 控件之dev报表 XtraReport (一) 初了解
这个控件其实用法和fast也差不了太多但如果没接触过 真有种老虎吃天的感觉 1.这里先不说那些高深的先说最基本的 在窗体中显示一个设计好的 模版 1.1一般设计和这个程序是分着的为了方便我就先把他们合 ...
- 关于scroll实现侧边导航栏
需求为一个简单的scroll效果,侧边选项卡跟随屏幕向下拖动变颜色的.点击侧边选项卡,跳转到相应模块. 索性上网找了一下类似的效果.附带源码地址 https://blog.csdn.net/drea ...
- CentOS6.5下RPM包安装MySQL5.6.35
基本环境:VMware10.0+CentOS6.5+MySQL5.6.35 .查看操作系统相关 shell> cat /etc/issue shell> uname -a .创建需要下载r ...
- 关于windows和linux系统更换JDK版本后,修改环境变量也无法生效的原因和解决办法
今天遇到了一个问题: 我linux系统之前安装JDK12,今天将其改成了JDK1.8,并修改了环境变量,但是通过java -version命令显示的依旧是JDK12的版本. 这是因为,当使用安装版本的 ...
- 安装 Confluence6.7.1 笔记
安装 Confluence6.7.1 我是使用命令行进入mysql的mysql -uroot -p123456 创建数据库 CREATE DATABASE confluence DEFAULT CHA ...
- Python 分解质因数
def zys(n, value=[]): for i in range(2, int(n / 2 + 1)): if n % i == 0: value.append(i) zys(n / i, v ...