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;

}

最新文章

  1. javaScript中值类型通过typeof直接进行检测
  2. bookstrap必备的基础知识
  3. 【iCore3 双核心板】例程十二:通用定时器实验——定时点亮LED
  4. Template Method模式和Strategy模式[继承与委托]
  5. 九 EJB
  6. ecshop二次开发
  7. MAC安装XAMPP的出现无法打开Apache server
  8. OpenRTSP的使用
  9. Java CopyOnWriteArrayList分析
  10. MQTT协议之 Apache Apollo服务
  11. SimplePath 使用心得
  12. Webpack学习系列(二)
  13. html页面顶部出现一段空白,检查控制台发现body 下出现&amp;#65279字符,原因及解决办法
  14. mysql初学,mysql修改,mysql查找,mysql删除,mysql基本命令
  15. Flink 核心技术浅析(整理版)
  16. BeautifulSoup学习 之结构
  17. 面向对象的 __slots__
  18. NTP 服务器搭建
  19. php数组函数常见的那些
  20. git grade 版本下载及安装

热门文章

  1. Linux下使用java获取cpu、内存使用率
  2. 2、Locust压力测试 实战
  3. 分析由Python编写的大型项目(Volatility和Cuckoo)
  4. HCW 19 Team Round (ICPC format) H Houston, Are You There?(极角排序)
  5. Gerrit Handbook for Commercial Project
  6. 9-MySQL-Ubuntu-数据表中数据的修改(二)
  7. keras 或 tensorflow 调用GPU报错:Blas GEMM launch failed
  8. 驾驶心率和呼吸,疲劳检测系统,通过安全带,坐垫等内置sensor
  9. ubuntu下网页视频或音频无法播放
  10. HashMap是不是有序的?