通过 runtime 进行归档、解档很节省很多工作,我先贴一段常规的解归档的代码。
/**归档:当一个对象归档(写入)沙盒时用*/
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:self.access_token forKey:@"access_token"];
[aCoder encodeObject:self.expires_in forKey:@"expires_in"];
[aCoder encodeObject:self.uid forKey:@"uid"];
[aCoder encodeObject:self.create_Time forKey:@"create_Time"];
[aCoder encodeObject:self.name forKey:@"name"];
[aCoder encodeObject:self.avatar_hd forKey:@"avatar_hd"];
[aCoder encodeObject:self.synopsis forKey:@"synopsis"];
[aCoder encodeObject:self.followers_count forKey:@"followers_count"];
[aCoder encodeObject:self.friends_count forKey:@"friends_count"];
[aCoder encodeObject:self.statuses_count forKey:@"statuses_count"];
}
/**反归档:当从沙盒读取一个对象时调用*/
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
self.expires_in = [aDecoder decodeObjectForKey:@"expires_in"];
self.uid = [aDecoder decodeObjectForKey:@"uid"];
self.create_Time = [aDecoder decodeObjectForKey:@"create_Time"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.avatar_hd = [aDecoder decodeObjectForKey:@"avatar_hd"];
self.synopsis = [aDecoder decodeObjectForKey:@"synopsis"];
self.followers_count = [aDecoder decodeObjectForKey:@"followers_count"];
self.friends_count = [aDecoder decodeObjectForKey:@"friends_count"];
self.statuses_count = [aDecoder decodeObjectForKey:@"statuses_count"];
}
return self;
}
通过这种方式进行解归档很麻烦,如果需要进行归解档的属性变得很多,这种方式就不适用了, 这个时候就需要使用 runtime 来操作。
这是我自己在学习使用 runtime 自动解归档的思路及总结:
1. 遵守 NSSecureCoding 协议
2. 导入 #import <objc/runtime.h>
3. 归档操作
- a) 获取所有实例变量
- b) 遍历所有实例变量
- c) 获取实例变量名
- d) 通过
KVC
获取value
- e) 进行编码
- f) 手动管理内存
4. 解档操作
- a) 获取所有实例变量
- b) 遍历所有实例变量
- c) 获取实例变量名
- d) 进行解码
- d) 通过
KVC
设置value
- f) 手动管理内存
总结:关键的地方在于获取实例变量名,然后再通过 KVC 进行操作。
我这里用了 NSSecureCoding
,没有用 NSCoding
,是因为 NSSecureCoding
协议继承自 NSCoding
,有着比 NSCoding
更加安全的编码和解码。
而且,在进行解归档的时候,原来的方法 archiveRootObject, unarchiveObjectWithFile
在 iOS 11 几以上是过时的方法,为了更好的适配,就使用了NSSecureCoding
,因为是继承关系,所以NSCoding
能用的NSSecureCoding
也能用。
需要注意的是,在使用 NSSecureCoding
的时候要实现supportsSecureCoding
这个方法,返回 YES。还有原来的解码decodeObjectForKey
也要换成另一种姿势。用这个- (nullable id)decodeObjectOfClasses:(nullable NSSet<Class> *)classes forKey:(NSString *)key
。
代码实现:
//
// Apply.h
// 01-RuntimeSendMessage
//
// Created by Mac on 2019/11/1.
// Copyright © 2019 Mac. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Apply : NSObject<NSSecureCoding>
@property(nonatomic, copy) NSString *name;
@property(nonatomic, strong) NSNumber *age;
@property(nonatomic, copy) NSString *nick;
@end
NS_ASSUME_NONNULL_END
//
// Apply.m
// 01-RuntimeSendMessage
//
// Created by Mac on 2019/11/1.
// Copyright © 2019 Mac. All rights reserved.
//
#import "Apply.h"
#import <objc/runtime.h>
@implementation Apply
// 归档的时候,系统会使用编码器把当前对象编码成二进制流
- (void)encodeWithCoder:(NSCoder *)coder {
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
id value = [self valueForKey:key];
// 编码
[coder encodeObject:value forKey:key];
}
// 因为是 C 语言的东西,不会自动释放,所以这里需要手动释放。
free(ivars);
}
// 解档的时候,系统会把二进制流解码成对象
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super init];
if (self) {
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 decodeObjectOfClasses:[NSSet setWithObject:[self class]] forKey:key];
// KVC
[self setValue:value forKey:key];
}
free(ivars);
}
return self;
}
+ (BOOL)supportsSecureCoding {
return YES;
}
@end
在使用的时候:
// 4.自动解归档
Apply *apply = [Apply new];
apply.name = @"张三";
apply.age = @18;
apply.nick = @"zhangsan";
Apply *apply_2;
NSString *fileName = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:@"archive.plist"];
if (@available(iOS 11.0, *)) {
NSData *data_1 = [NSKeyedArchiver archivedDataWithRootObject:apply requiringSecureCoding:YES error:nil];
[data_1 writeToFile:fileName atomically:YES];
NSData *data_2 = [[NSData alloc] initWithContentsOfFile:fileName];
apply_2 = [NSKeyedUnarchiver unarchivedObjectOfClass:[Apply class] fromData:data_2 error:nil];
} else {
[NSKeyedArchiver archiveRootObject:apply toFile:fileName];
apply_2 = [NSKeyedUnarchiver unarchiveObjectWithFile:fileName];
}
NSLog(@"name: %@, age: %@, nick: %@", apply_2.name, apply_2.age, apply_2.nick);
看一下打印效果:
通过打印已经知道自动解归档已经实现了!