- #import
-
- NS_ASSUME_NONNULL_BEGIN
-
- @interface HSFileModel : NSObject
-
- @property (nonatomic, strong) NSURL *fileUrl; //文件链接
- @property (nonatomic, copy) NSString *fileName; //文件名
-
- @end
@property (nonatomic, strong) NSMutableDictionary<NSString *, HSFileModel *> *selectedFilesData;
保存的数据到本地的方法
- // 保存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.读取刚才保存的数据,确保读取的数据的文件路径跟保存的文件路径一致。
- - (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协议
- #import
-
- NS_ASSUME_NONNULL_BEGIN
-
- @interface HSFileModel : NSObject <NSSecureCoding>
-
- @property (nonatomic, strong) NSURL *fileUrl; //文件链接
- @property (nonatomic, copy) NSString *fileName; //文件名
-
- @end
- #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
使用这个方法解档的话,参数(NSSet
self.selectedFilesData = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithArray:@[NSMutableDictionary.class, NSString.class, HSFileModel.class, NSURL.class]] fromData:data error:&error];
到此结束,如大佬有补充请指出。