归档 & 解档

2019-03-01  本文已影响0人  wxhan

1.什么是 归档 和 解档

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

2.具体过程

#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject<NSCoding>
@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *age;
@property (nonatomic,copy)NSString *number;
@end
NS_ASSUME_NONNULL_END
#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

(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 <objc/runtime.h>

@implementation Person

- (NSString *)description{
    unsigned int count;
    Ivar *ivar = class_copyIvarList([self class], &count);
    NSString *descString = @"打印:";
    for (int i = 0; i<count; i++) {
        Ivar iva = ivar[i];
        const char *name = ivar_getName(iva);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        descString = [NSString stringWithFormat:@"%@\n%@",descString,value];
    }
    free(ivar);
    return descString;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
    unsigned int count;
    Ivar *ivar = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {
        Ivar iva = ivar[i];
        const char *name = ivar_getName(iva);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        [aCoder encodeObject:value forKey:strName];
    }
    free(ivar);
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super init];
    if (self) {
        unsigned int count = 0;
        Ivar *ivar = class_copyIvarList([self class], &count);
        for (int i = 0; i < count; i++) {
            Ivar iva = ivar[i];
            const char *name = ivar_getName(iva);
            NSString *strName = [NSString stringWithUTF8String:name];
            //进行解档取值
            id value = [aDecoder decodeObjectForKey:strName];
            //利用KVC对属性赋值
            [self setValue:value forKey:strName];
        }
        free(ivar);
    }
    return self;
}
@end
上一篇 下一篇

猜你喜欢

热点阅读