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 小时前
一晃13年过去了,苹果的Airdrop依然很坚挺。
macos·ios·苹果vision pro
普通网友12 小时前
苹果笔记本(Mac)连接手机完全指南
macos·智能手机
锐意无限13 小时前
Swift 扩展归纳--- UIView
开发语言·ios·swift
符哥200813 小时前
用Apollo + RxSwift + RxCocoa搭建一套网络请求框架
网络·ios·rxswift
Aftery的博客14 小时前
Xcode运行报错:SDK does not contain ‘libarclite‘ at the path
macos·cocoa·xcode
文件夹__iOS18 小时前
AsyncStream 进阶实战:SwiftUI 全局消息流极简实现
ios·swiftui·swift
2501_9160088920 小时前
深入解析iOS机审4.3原理与混淆实战方法
android·java·开发语言·ios·小程序·uni-app·iphone
忆江南20 小时前
Flutter深度全解析
ios
山水域20 小时前
Swift 6 严格并发检查:@Sendable 与 Actor 隔离的深度解析
ios
楚轩努力变强21 小时前
iOS 自动化环境配置指南 (Appium + WebDriverAgent)
javascript·学习·macos·ios·appium·自动化