1. 程式人生 > >歸檔和解檔配合NSFile存儲數據

歸檔和解檔配合NSFile存儲數據

反序列化 ins super oms char nbsp search 搜索 del

 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存儲數據