-
利用OC提供的
解归档
来实现自定义模型缓存,这里模型遵循<NSSecureCoding>协议
。
-
<NSSecureCoding>
基于<NSCoding>
,相对更加安全。
方案一:(常规写法:字段比较多的话,一个一个编解码比较麻烦)
Person.h文件
@interface Person : NSObject <NSSecureCoding> //解归档需要遵循NSSecureCoding协议,并实现相关方法
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger sex;
@property (nonatomic, assign) NSInteger age;
@end
Person.m文件
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeInteger:self.sex forKey:@"sex"];
[aCoder encodeInteger:self.age forKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.sex = [aDecoder decodeIntegerForKey:@"sex"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
@end
方案二:(利用YYModel处理编解码)
Person.h文件
@interface Person : NSObject <NSSecureCoding> //解归档需要遵循NSSecureCoding协议,并实现相关方法
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger sex;
@property (nonatomic, assign) NSInteger age;
@end
Person.m文件
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder {
[self yy_modelEncodeWithCoder:aCoder];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
return [self yy_modelInitWithCoder:aDecoder];
}
+ (BOOL)supportsSecureCoding {
return YES;
}
@end
方案三:(利用runtime处理编解码,要导入头文件#import <objc/runtime.h>
)
Person.h文件
@interface Person : NSObject <NSSecureCoding> //解归档需要遵循NSSecureCoding协议,并实现相关方法
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger sex;
@property (nonatomic, assign) NSInteger age;
@end
Person.m文件
@implementation Person
- (void)encodeWithCoder:(NSCoder *)aCoder {
unsigned int count = 0;
// 获取所有实例变量
Ivar *ivars = class_copyIvarList([self class], &count);
// 遍历所有实例变量
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[I];
// 获取实例变量名
const char *name = ivar_getName(ivar);
NSString *key = [NSString stringWithUTF8String:name];
// 通过KVC获取value值
id value = [self valueForKey:key];
// 编码
[coder encodeObject:value forKey:key];
}
free(ivars); // C不会自动释放,需要手动释放。
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
unsigned int count = 0;
// 获取所有实例变量
Ivar *ivars = class_copyIvarList([self class], &count);
// 遍历所有实例变量
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
// 获取实例变量名
const char *name = ivar_getName(ivar);
NSString *key = [NSString stringWithUTF8String:name];
// 解码
id value = [coder decodeObjectOfClass:[self class] forKey:key];
// 通过KVC 设置value
[self setValue:value forKey:key];
}
free(ivars); // C不会自动释放,需要手动释放。
}
return self;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
@end
ViewController.m文件
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person *person1 = [Person new];
person1.name = @"程序猿";
person1.sex = 1;
person1.age = 26;
Person *person2 = [Person new];
person2.name = @"程序媛";
person2.sex = 0;
person2.age = 27;
NSMutableArray *personArr = [NSMutableArray arrayWithCapacity:0];
[personArr addObject:person1];
[personArr addObject:person2];
// 这里以缓存模型数组为例,缓存单个模型对象,直接把对应模型数组参数改为模型对象即可(存取都要更改)
[self wirteToFile: personArr]; // 归档
[self readFromFile]; // 解档
}
// 归档
- (void)wirteToFile:(NSArray *)dataArray {
NSString *fileName = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"archive.person"];
if (@available(iOS 11.0, *)) {
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:dataArray requiringSecureCoding:YES error:nil];
[arrayData writeToFile:fileName atomically:YES];
} else {
NSData *arrayData = [NSKeyedArchiver archivedDataWithRootObject:dataArray];
[arrayData writeToFile:fileName atomically:YES];
}
}
// 解档
- (void)readFromFile {
NSString *fileName = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"archive.person"];
NSData *resultData = [[NSData alloc]initWithContentsOfFile:fileName];
// 解档模型数组使用unarchivedArrayOfObjectsOfClass: fromData: error: 此方法iOS 14.0可用
// 解档单个模型数据可以使用 unarchivedObjectOfClass: fromData: error:,iOS 11.0可用
if (@available(iOS 14.0, *)) {
NSArray *resultArray = [NSKeyedUnarchiver unarchivedArrayOfObjectsOfClass:[Person class] fromData:resultData error:nil];
NSLog(@"%@",resultArray);
} else {
NSArray *resultArray = [NSKeyedUnarchiver unarchiveObjectWithData:resultData];
NSLog(@"%@",resultArray);
}
}
@end