归档和解档配合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;

}

时间: 2024-07-30 22:33:01

归档和解档配合NSFile存储数据的相关文章

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

一.归档介绍 1.归档是指用某种格式来保存一个或多个对象,以便以后还原这些对象的过程.归档是将数据持久化的一种方式(所谓数据持久化,就是指在IOS开发过程中,将数据保存到本地,能够让程序的运行更加流畅). 2.想要归档的数据对象,需要遵守NSCoding协议,并且该对象对应的类必须提供encodeWithCoder:和initWithCoder:方法. 3.归档就是将临时数据保存成本地文件. 4.归档的缺点:归档的形式来保存数据,只能一次性归档保存以及一次性解压.所以只能针对小量数据,而且对数据

【非凡程序员】 OC第十七节课 文件操作二 (归档和解档)

//-----------------------------归档和解档-----(重点)-------.-----------//        //可变的文件流        NSMutableData *nutabdata=[[NSMutableData alloc]init];        //把用归档格式的数据值给可变的文件流        NSKeyedArchiver *keyde=[[NSKeyedArchiver alloc]initForWritingWithMutable

数据存取,归档和解档,偏好设置

// --- 沙盒路径 // 如何获取沙盒的根目录 NSString* path = NSHomeDirectory(); // 快速查看沙盒目录 // SimPholders2 // 如何获取 doc 路径 NSString* docpath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; // 获取tmp路径 NSString* tmpPath = NSTemporar

归档和解档---秀清

// // AccountTool.h // // Created by Joe Zhang on 15/5/23. // Copyright (c) 2015年 张秀清. All rights reserved. // #import <Foundation/Foundation.h> #import "Account.h" @interface AccountTool : NSObject //存储账号 +(void)save:(Account *)account; /

归档和解档

归档--encoder    它是将自定义的对象写入磁盘前将对象转成二进制然后存入磁盘. 解档--decoder    它是将磁盘上保存的二进制数据转换成自定义对象. eg: 自定义一个person对象 .h 文件中 @property (nonatomic,copy)NSString *name; @property (nonatomic,assign)int age; .m 文件中 // 归档 - (void)encodeWithCode:(NSCode *)encode { [encode

浅谈runtim的归档和解档

自定义一个Person类 Person.h里面 #import <Foundation/Foundation.h> //遵循一个NSCoding协议 @interface Person : NSObject<NSCoding> //定义三个person类的属性 @property(strong,nonatomic)NSString *name; @property(assign,nonatomic)int age; @property(strong,nonatomic)NSStri

iOS 文件操作--归档和解档

把自己定义的类所创建的对象直接写入文件的步骤: 自定义类遵循NSCoding协议,实现NSCoding协议中的两个方法: encodeWithCoder:往文件中写入实例变量 initWithCoder:从文件中读取实例变量为当前对象赋值 如果把对象写入文件:调用NSKeyedArchiver中的archiveRootObject:toFile: 如果把对象从文件中读取出来:调用NSKeyedUnarchiver中的unarchiveObjectWithFile:

数据存储之归档解档

归档也叫序列化,是将文件存在硬盘,解码是从硬盘还原一.使用属性列表进行归档 如果对象是NSString,NSDictionary,NSArray,NSData,NSNumber,NSDate,可以是使用writeToFile:atomically方法将数据写到文件,注意这种方式是明文. NSArray *inputArray = [NSArray arrayWithObjects:@"abc", @"123", @"qiaohaibin"]; /

OC学习篇之---归档和解挡

前几篇文章说到了OC中的Foundation框架:http://blog.csdn.net/jiangwei0910410003/article/details/41852835,今天我们来看一下OC中的一个重要知识点:归档 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些对象都是需要实现一个接口:Serializable,同样的OC中操作的对象也是需要实现一个协议的,后面会说到. 一.已有