iOS NSKeyedUnarchiver归档和读取

使用NSKeyedUnarchiver归档数据到本地,很多时候保存的并不是基础数据类型,更多是自己定义的Model。有时会碰到归档或者读取的内容跟自己保存的数据类型不匹配。

现在按照思路一步一步解决:
1.先保存文件
保存的数据的类型

objectivec 复制代码
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface HSFileModel : NSObject 

@property (nonatomic, strong) NSURL *fileUrl; //文件链接
@property (nonatomic, copy) NSString *fileName; //文件名

@end
objectivec 复制代码
@property (nonatomic, strong) NSMutableDictionary<NSString *, HSFileModel *> *selectedFilesData;

保存的数据到本地的方法

objectivec 复制代码
// 保存selectedFilesData到本地文件
- (void)saveSelectedFilesDataToLocal {
    // 获取文件路径
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    // 拼接文件路径
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"SelectedFilesData.plist"];
    
    // 归档字典对象
    NSError *error = nil;
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.selectedFilesData requiringSecureCoding:YES error:&error];
    
    if (error) {
        NSLog(@"Error archiving data: %@", error);
    } else {
        // 将归档数据写入文件
        [data writeToFile:filePath atomically:YES];
    }
}

2.读取刚才保存的数据,确保读取的数据的文件路径跟保存的文件路径一致。

objectivec 复制代码
- (void)loadSelectedFilesDataFromLocal {
    // 获取文件路径
    // 获取文件路径
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    
    // 拼接文件路径
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"SelectedFilesData.plist"];

    // 尝试从文件中读取归档数据
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    if (data) {
        // 解档数据为字典对象
        NSError *error = nil;
        self.selectedFilesData = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[NSMutableDictionary.class, NSString.class, HSFileModel.class, NSURL.class]] fromData:data error:&error];
        
        
        if (error) {
            NSLog(@"Error unarchiving data: %@", error);
            // 可以在此处理解档错误的情况
        }
    } else {
        // 如果文件不存在或读取失败,可以初始化一个空字典
        self.selectedFilesData = [NSMutableDictionary dictionary];
    }
}

当调用读取的方法的时候会有一个错误如下:

Printing description of error:

Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class 'HSFileModel' does not adopt it." UserInfo={NSDebugDescription=This decoder will only decode classes that adopt NSSecureCoding. Class 'HSFileModel' does not adopt it.}

这因为保存的数据类型有自己定义的Model,而且HSFileModel没有实现NSSecureCoding协议导致不能解码。所有被编码和解码的类都必须遵循NSSecureCoding协议。

3.给HSFileModel实现NSSecureCoding协议

objectivec 复制代码
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface HSFileModel :  NSObject <NSSecureCoding> 

@property (nonatomic, strong) NSURL *fileUrl; //文件链接
@property (nonatomic, copy) NSString *fileName; //文件名

@end
objectivec 复制代码
#import "HSFileModel.h"

@implementation HSFileModel
+ (BOOL)supportsSecureCoding {
    return YES;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.fileUrl forKey:@"fileUrl"];
    [coder encodeObject:self.fileName forKey:@"fileName"];
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super init];
    if (self) {
        self.fileUrl = [coder decodeObjectForKey:@"fileUrl"];
        self.fileName = [coder decodeObjectForKey:@"fileName"];
    }
    return self;
}


@end

4.对于 + (nullable id)unarchivedObjectOfClasses:(NSSet<Class> *)classes fromData:(NSData *)data error:(NSError **)error

使用这个方法解档的话,参数(NSSet<Class> *)classes应该传入目标数据可能包含的数据的数据类型的集合。比如:

objectivec 复制代码
   self.selectedFilesData = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[NSMutableDictionary.class, NSString.class, HSFileModel.class, NSURL.class]] fromData:data error:&error];

到此结束,如大佬有补充请指出。

相关推荐
夏幻灵8 小时前
HTTPS全面解析:原理、加密机制与证书体
ios·iphone
TheNextByte112 小时前
如何在iPhone上恢复已删除的笔记的综合指南
笔记·ios·iphone
June bug14 小时前
【领域知识】广告全链路测试
macos·objective-c·cocoa
rose and war14 小时前
python和jinja版本问题导致的访问报500
python·ios
fendoudexiaoniao_ios14 小时前
iOS 列表拖拽cell排序
ios·swift
2501_9151063215 小时前
当 Perfdog 开始收费之后,我重新整理了一替代方案
android·ios·小程序·https·uni-app·iphone·webview
作孽就得先起床16 小时前
Xcode设置中文
macos
smileSunshineMan16 小时前
本地mac-openclaw安装
macos·openclaw
2501_9159184117 小时前
中小团队发布,跨平台 iOS 上架,证书、描述文件创建管理,测试分发一体化方案
android·ios·小程序·https·uni-app·iphone·webview
fish_study_csdn18 小时前
自己的mac 电脑上部署本地大模型
macos