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存储数据的更多相关文章

  1. 【IOS学习基础】归档和解档

    一.归档介绍 1.归档是指用某种格式来保存一个或多个对象,以便以后还原这些对象的过程.归档是将数据持久化的一种方式(所谓数据持久化,就是指在IOS开发过程中,将数据保存到本地,能够让程序的运行更加流畅 ...

  2. OC 归档和解档

    #import <Foundation/Foundation.h> #define PATH @"/Users/mac/Desktop/file.txt" int ma ...

  3. 利用Runtime对Ivar实例变量进行共用的归档和解档方式

    一.介绍 在OC中每一个对象持有的变量都是实例变量,实例变量包括成员变量和属性变量,在runtime中用Ivar表示对象的实例变量.其实,runtime源码中可以看到,Ivar也是一个结构体(基本上在 ...

  4. runtime之归档和解档

    IOS开发之NSCoding协议(使用runtime)近期学习IOS的runtime库,然后看到之前写的NSCoding协议有点复杂,如果属性少还好,如果100多个属性,则会显得麻烦.下面使用常规方式 ...

  5. iOS基础知识之归档和解档

    归档和解档:即将数据写入文件和从文件中读取数据. 此处以plist文件为例说明, 一.plist文件使用时的注意事项: 1.plist文件中仅支持写入Array,Dictionary,Boolean, ...

  6. 数据存储之归档解档 NSKeyedArchiver NSKeyedUnarchiver

    在构建应用程序时,有一个重要的问题是如何在每次启动之间持久化数据,以便重现最后一次关闭应用前的状态.在iOS和OS X上,苹果提供了三种选择:Core Data.属性列表(Property List) ...

  7. iOS 数据存储 - 归档和解归档

    这里的归档主要是用于自定义类的归档和解档.我们这里使用NSKeyedArchiver和NSKeyedUnarchiver来归档和解档. 注意:自己定义的类需要实现<NSCoding>,如: ...

  8. iOS开发中的4种数据持久化方式【一、属性列表与归档解档】

    iOS中的永久存储,也就是在关机重新启动设备,或者关闭应用时,不会丢失数据.在实际开发应用时,往往需要持久存储数据的,这样用户才能在对应用进行操作后,再次启动能看到自己更改的结果与痕迹.ios开发中, ...

  9. iOS--归档和解档(Archiver)、(UnArchiver)

    一.已有类型的归档和解档 首先来看一个简单的例子: //第一方式:归档对象 //对象-->文件 NSArray *array = [NSArray arrayWithObjects:@" ...

随机推荐

  1. angularJS 上传multipart/form-data

    var fd = new FormData();fd.append('file', vm.file);CommodityViewImport.post(fd, onSaveSuccess, onSav ...

  2. js小项目:显示与输入的内容相关的

    1,添加键盘抬起事件 2,获取文本框的内容,是否与数组中的内容匹配 3,创建元素 <!DOCTYPE html> <html lang="en"> < ...

  3. python获取全部股票每日基本面指标,用于选股分析、报表展示等

    接口:daily_basic 更新时间:交易日每日15点-17点之间 描述:获取全部股票每日重要的基本面指标,可用于选股分析.报表展示等. 积分:用户需要至少300积分才可以调取,具体请参阅本文最下方 ...

  4. python--面向对象:多态与封装

    一.多态 :python 天生支持多态多态指的是一类事物有多种形态 eg:文件有多种形态:文本文件,可执行文件鸭子类型:python中崇尚鸭子类型,不崇尚根据继承所得来的相似 优点 : 松耦合 每个相 ...

  5. 一.Python特点

    python第一节 简介 Python介绍 什么样的语言? 解释性语言:开发中没有编译的环节 交互式语言:在命令提示行执行python引擎,直接执行代码 面向对象语言:支持面向对象 优点 a.易学 b ...

  6. java oop第10章_JDBC03(MVC分层模式)

    引言:在进行程序开发的时候,为了更加利于程序的管理我们引入了新的开发模式MVC分层模式,即按功能将程序代码分别分为M(Model模型).V(View视图).C(Controller控制器)三个组成部分 ...

  7. Arrays 001

    1.1 Array Initalization First of all, we need know Java arrays is static. When the array is initiali ...

  8. while循环和for循环

    while循环 n='我怎么那么可爱呀' count= while count<len(n): print(n[count]) count+= for循环 n='我怎么那么可爱呀' for i ...

  9. vue $emit 子传父

    我们使用子组件传递值给父组件使用 $emit 代码 <!DOCTYPE html> <html lang="en"> <head> <me ...

  10. Hexo next主题添加站内搜索功能

    根据关键字搜索博文,站内搜索的功能很实用.hexo开启站内搜索很方便,已经有现成的插件可以使用,也是为了方便自己 安装插件 npm install hexo-generator-search --sa ...