归档 & 解档

1.什么是 归档 和 解档

数据本地存储持久化的一种。
归档:对象的序列化,通过某种格式把对象保存成本地文件。
解档:反序列化,把归档的对象文件读成原来的对象。

2.具体过程

  • 在归档对象的.h中遵循规定协议
#import 
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *age;
@property (nonatomic,copy)NSString *number;
@end
NS_ASSUME_NONNULL_END
  • 在归档对象的.m中实现协议方法
#import "Person.h"
@implementation Person
- (NSString *)description{
    return [NSString stringWithFormat:@"%@\n%@\n%@",_name,_age,_number];
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
    // 告诉系统归档的属性是哪些
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.age forKey:@"age"];
    [aCoder encodeObject:self.number forKey:@"number"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        // 解档
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [aDecoder decodeObjectForKey:@"age"];
        self.number = [aDecoder decodeObjectForKey:@"number"];
    }
    return self;
}
@end
  • vc中进行存取操作(以temp路径为例)

(1)归档过程

Person *p = [[Person alloc] init];
p.name = @"wxh";
p.age = @"18岁";
p.number = @"23号";
NSString *filePath = NSTemporaryDirectory();
NSString *uniquePath = [filePath stringByAppendingPathComponent:@"p_wxh.plist"];
[NSKeyedArchiver archiveRootObject:p toFile:uniquePath];

(2)解档过程

NSString *filePath = NSTemporaryDirectory();
NSString *uniquePath = [filePath stringByAppendingPathComponent:@"p_wxh.plist"];
Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:uniquePath];
NSLog(@"%@",p);

(3)删除文件

- (void)deleteFileWithFileName:(NSString *)fileName filePath:(NSString *)filePath {
    //创建文件管理对象
    NSFileManager* fileManager = [NSFileManager defaultManager];
    //获取文件目录
    if (!filePath) {
        //如果文件目录设置有空,默认删除Cache目录下的文件
        filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
    }
    //拼接文件名
    NSString *uniquePath=[filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",fileName]];
    //文件是否存在
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
    //进行逻辑判断
    if (!blHave) {
        NSLog(@"文件不存在");
        return ;
    }
    else {
        // 文件是否被删除
        BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
        // 进行逻辑判断
        if (blDele) {
            NSLog(@"删除成功");
        }
        else {
            NSLog(@"删除失败");
        }
    }
}

(4)获取文件路径有三种方法

// 获取沙盒Document路径
filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
// 获取沙盒temp路径
filePath = NSTemporaryDirectory();
// 获取沙盒Cache路径
filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
// 文件路径
NSString *uniquePath = [filePath stringByAppendingPathComponent:你的文件名称];

3.使用runtime

当在归档对象.m中实现协议方法时,如果对象的属性比较多,这个时候就会很繁琐。那么可以利用runtime获取属性,简化过程。

#import "Person.h"
#import 

@implementation Person

- (NSString *)description{
    unsigned int count;
    Ivar *ivar = class_copyIvarList([self class], &count);
    NSString *descString = @"打印:";
    for (int i = 0; i

你可能感兴趣的:(归档 & 解档)