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

}

 

posted @   小师傅啊小师傅  阅读(164)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
点击右上角即可分享
微信分享提示