归档和解档配合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:@" ...
随机推荐
- HTML a标签文字颜色
1.css代码: a{color:#00F} a:hover{color:#f00}/* 鼠标经过悬停字体颜色 */ /* css 注释说明:以上代码为设置HTML中超链接统一字体颜色 */ .div ...
- PAT_A1083#List Grades
Source: PAT A1083 List Grades (25 分) Description: Given a list of N student records with name, ID an ...
- vmstat - 报告虚拟内存的统计信息
总览 vmstat [-n] [延时[次数]] vmstat [-V] 描述 vmstat 对系统的进程情况.内存使用情况.交换页和 I/O 块使用情况.中断以及 CPU 使用情况进行统计并报告相应的 ...
- 关于vsphere exsi安装时遇到的问题
我在虚拟机上安装vsphere 6.0的时候,在安装的过程中报错,出现了如下的内容. Error loading /tools.t00 Compressed MD5: Decompressed MD5 ...
- tomcat+apache+jk
安装JDK下载地址 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html安装 rpm ...
- ubuntu查看时间同步服务器的匹配源
当服务器时间与设定好的同步时间源的时间有差异的时候,一般都需要先查看本机的时间同步服务功能是否在正常的运转,以及同步的时间源是哪里,在这里为大家提供一个检查时间用的命令. ubuntu版本 servi ...
- 50. Set接口和Set的实现类HashSet
集合分类:-------------------| Collection 单列集合的根接口 ---------------| List 如果实现了List接口的集合类,具备的特点是:有序,可重复- ...
- jQuery.speech实现文本转语音播报功能
先放一个实例的地址https://github.com/wenco/speech jQuery.speech是用jQuery写的扩展插件,主要是用来语音播报. 接口调用百度翻译的接口,所以存在url参 ...
- NX二次开发-UFUN更改图纸页比例UF_DRAW_set_drawing_info
#include <uf.h> #include <uf_draw.h> #include <uf_part.h> UF_initialize(); //获得当前图 ...
- NX二次开发-UFUN打开二进制STL文件函数UF_STD_open_binary_stl_file
NX9+VS2012 #include <uf.h> #include <uf_obj.h> #include <uf_modl.h> #include <u ...