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];

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

相关推荐
五阿哥爱跳舞11 小时前
MAC备忘录空白解决方案
macos
乌恩大侠13 小时前
【Xcode Command Line Tools】安装指南
macos·fpga开发·c
missmisslulu14 小时前
电容笔值得买吗?2024精选盘点推荐五大惊艳平替电容笔!
学习·ios·电脑·平板
GEEKVIP15 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
GEEKVIP15 小时前
如何在 Windows 10 上恢复未保存/删除的 Word 文档
macos·ios·智能手机·电脑·word·笔记本电脑·iphone
奇客软件16 小时前
iPhone使用技巧:如何恢复变砖的 iPhone 或 iPad
数码相机·macos·ios·电脑·笔记本电脑·iphone·ipad
前端张三17 小时前
Mac 电脑pink 后端ip地址进行本地联调
服务器·tcp/ip·macos
缘友一世1 天前
macos安装mongodb
数据库·mongodb·macos
笑非不退1 天前
macOS开发环境配置与应用开发
macos
colorknight2 天前
1.2.3 HuggingFists安装说明-MacOS安装
人工智能·低代码·macos·huggingface·数据科学·ai agent