数据持久化方案三----归档/解归档

写在前方 笔者会用到之前文章的方法(自己封装下)

注意:好久没有使用归解档,一片warning。
貌似NSKeyedArchiver/NSKeyedUnarchiver被苹果重新折腾了个遍,使用安全归解档,从iOS11-12起废弃了不少方法,开发者需要重阅api。

一、归档(NSKeyedArchiver)(序列化/编码),把对象转为字节码,以文件的形式存储到磁盘上;程序运行过程中或者当再次重写打开程序的时候,可以通过解归档(NSKeyedUnarchiver)(反序列化/解码)还原这些对象。

二、支持的存储数据类型
几乎任意类型,同时归档后的文件是加密的,所以归档文件的扩展名可以随意取。

三、相关代码
(一)、归解档准备类,遵循NSSecureCoding协议,实现三个方法:

- (void)encodeWithCoder:(NSCoder *)aCoder;
- (instancetype)initWithCoder:(NSCoder *)aDecoder;
+ (BOOL)supportsSecureCoding 

model类如下:

#import 

NS_ASSUME_NONNULL_BEGIN

@interface XLOneModel : NSObject 

@property (nonatomic, strong) NSString *school;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;

@end

NS_ASSUME_NONNULL_END



#import "XLOneModel.h"


@implementation XLOneModel

/*
 *MARK: -编码
 */
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.school forKey:@"school"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeInteger:self.age forKey:@"age"];
}

/*
 *MARK: -解码
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.school = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"school"];
        self.name   = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"name"];
        self.age    = [aDecoder decodeIntegerForKey:@"age"];
    }
    return self;
}

/*
 *MARK: -支持安全编码
 */
+ (BOOL)supportsSecureCoding {
    return YES;
}

@end

(二)、归档,解归档实现

//1、创建地址

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *archiverPath = [docPath stringByAppendingPathComponent:@"myFile/test.xlArchiver"];
    //见"写在前方"
    [XLFileManagerTool creatFileWithPath:archiverPath];

//2、创建待归档的对象

    XLOneModel *archiverModel = [[XLOneModel alloc] init];
    archiverModel.school = @"第一中学";
    archiverModel.name = @"张三";
    archiverModel.age = 15;

//3、归档

    if (@available(iOS 11.0, *)) {
        //使用NSData存放归档数据
        NSError *error;
        NSData *archiverData = [NSKeyedArchiver archivedDataWithRootObject:archiverModel requiringSecureCoding:YES error:&error];
        if (error) {
            NSLog(@"Archiver Failed, errorInfo:%@", error);
        }
        BOOL isSuccess = [archiverData writeToFile:archiverPath atomically:YES];
        if (isSuccess) {
            NSLog(@"archiver success");
        }
    
    } else {
        // Fallback on earlier versions
        NSData *archiverData = [NSKeyedArchiver archivedDataWithRootObject:archiverModel];
        BOOL isSuccess = [archiverData writeToFile:archiverPath atomically:YES];
        if (isSuccess) {
            NSLog(@"archiver success");
        }
    }

//4、解档

    NSData *unArchivedData = [NSData dataWithContentsOfFile:archiverPath];
    if (@available(iOS 11.0, *)) {
        NSError *error;
        XLOneModel *unArchivedModel = [NSKeyedUnarchiver unarchivedObjectOfClass:[XLOneModel class] fromData:unArchivedData error:&error];
        if (unArchivedModel == nil || error) {
            NSLog(@"解档失败:%@", error);
        } else {
            NSLog(@"解档成功:%@ , %@, %ld",unArchivedModel.school, unArchivedModel.name,unArchivedModel.age);
        }
    
    } else {
        NSError *error;
        XLOneModel *unArchivedModel = [NSKeyedUnarchiver unarchiveObjectWithData:unArchivedData];
        if (unArchivedModel == nil) {
            NSLog(@"解档失败:%@", error);
        } else {
            NSLog(@"解档成功:%@ , %@, %ld",unArchivedModel.school, unArchivedModel.name,unArchivedModel.age);
        }
    }

你可能感兴趣的:(数据持久化方案三----归档/解归档)