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

}

 

转载于:https://www.cnblogs.com/wycstudy/p/7815495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值