方法一
编解码的核心就是:遍历该类的属性变量、实例变量及其父类的属性变量(父类的实例变量为其私有,我们如果对其进行编解码会崩溃),这时候就用到运行时的api了
敲重点:
Class cls = [self class];
BOOL isSelfClass = (cls == [self class]);
objc_property_t *propertyList = isSelfClass ? NULL : class_copyPropertyList(cls, &propertyCount);
Ivar *ivarList = isSelfClass ? class_copyIvarList(cls, &iVarCount) :NULL;
解释一下,首先判断是不是本类,如果是本类,就使用class_copyIvarList获取属性变量和实例变量;
如果是父类,就使用class_copyIvarList获取父类的属性变量。
//解码
- (id)initWithCoder:(NSCoder *)coder
{
Class cls = [self class]; while (cls != [NSObject class]) {
BOOL isSelfClass = (cls == [self class]);
unsigned int iVarCount = 0;
unsigned int propertyCount = 0;
objc_property_t *propertyList = isSelfClass ? NULL : class_copyPropertyList(cls, &propertyCount);
Ivar *ivarList = isSelfClass ? class_copyIvarList(cls, &iVarCount) :NULL;
unsigned int finalCount = isSelfClass ? iVarCount : propertyCount; for (int i = 0; i < finalCount; i++) { const char * varName = isSelfClass ? ivar_getName(*(ivarList + i)) :property_getName(*(propertyList + i));//取得变量名字,将作为key
NSString *key = [NSString stringWithUTF8String:varName]; //decode
id value = [coder decodeObjectForKey:key];//解码
NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; if (value && [filters containsObject:key] == NO) {
[self setValue:value forKey:key];//使用KVC强制写入到对象中
}
}
free(ivarList);//记得释放内存
free(propertyList);
cls = class_getSuperclass(cls);
} return self;
}
//编码
- (void)encodeWithCoder:(NSCoder *)coder
{
Class cls = [self class]; while (cls != [NSObject class]) {
BOOL isSelfClass = (cls == [self class]);
unsigned int varCount = 0;
unsigned int properCount = 0;
Ivar *ivarList = isSelfClass ? class_copyIvarList([self class], &varCount) : NULL;
objc_property_t *propertyList = isSelfClass ? NULL : class_copyPropertyList(cls, &properCount);
unsigned int finalCount = isSelfClass ? varCount : properCount; for (int i = 0; i < finalCount; i++) { const char *varName = isSelfClass ? ivar_getName(*(ivarList + i)) : property_getName(*(propertyList + i));
NSString *key = [NSString stringWithUTF8String:varName]; id varValue = [self valueForKey:key];//使用KVC获取key对应的变量值
NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; if (varValue && [filters containsObject:key] == NO) {
[coder encodeObject:varValue forKey:key];
}
}
free(ivarList);
free(propertyList);
cls = class_getSuperclass(cls);
}
}
另外,为一个类添加描述也可以同理进行处理,这样就可以很直观的看出容器里装的model类是怎样子的了:
/* 用来打印本类的所有变量(成员变量+属性变量),所有层级父类的属性变量及其对应的值 */
- (NSString *)description
{
NSString *despStr = @"";
Class cls = [self class]; while (cls != [NSObject class]) { /*判断是自身类还是父类*/ BOOL bIsSelfClass = (cls == [self class]);
unsigned int iVarCount = 0;
unsigned int propVarCount = 0;
unsigned int sharedVarCount = 0;
Ivar *ivarList = bIsSelfClass ? class_copyIvarList([cls class], &iVarCount) : NULL;/*变量列表,含属性以及私有变量*/ objc_property_t *propList = bIsSelfClass ? NULL : class_copyPropertyList(cls, &propVarCount);/*属性列表*/ sharedVarCount = bIsSelfClass ? iVarCount : propVarCount; for (int i = 0; i < sharedVarCount; i++) { const char *varName = bIsSelfClass ? ivar_getName(*(ivarList + i)) : property_getName(*(propList + i));
NSString *key = [NSString stringWithUTF8String:varName]; /*valueForKey只能获取本类所有变量以及所有层级父类的属性,不包含任何父类的私有变量(会崩溃)*/
id varValue = [self valueForKey:key];
NSArray *filters = @[@"superclass", @"description", @"debugDescription", @"hash"]; if (varValue && [filters containsObject:key] == NO) {
despStr = [despStr stringByAppendingString:[NSString stringWithFormat:@"%@: %@n", key, varValue]];
}
}
free(ivarList);
free(propList);
cls = class_getSuperclass(cls);
} return despStr;
}
方法二
其实作为归档来说,我们通常不需要讲父类全部属性一起归档。只需要通过解档来去除我们所需的数据、状态。因此以下为常用的更为简洁的代码:
#import "Student.h"
#import <objc/runtime.h>
#import <objc/message.h>
@implementation Student
- (void)encodeWithCoder:(NSCoder *)aCoder{
unsigned int outCount = 0;
Ivar *vars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i ++) {
Ivar var = vars[i];
const char *name = ivar_getName(var);
NSString *key = [NSString stringWithUTF8String:name];
id value = [self valueForKey:key];
[aCoder encodeObject:value forKey:key];
}
}
- (nullable __kindof)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
unsigned int outCount = 0;
Ivar *vars = class_copyIvarList([self class], &outCount);
for (int i = 0; i < outCount; i ++) {
Ivar var = vars[i];
const char *name = ivar_getName(var);
NSString *key = [NSString stringWithUTF8String:name];
id value = [aDecoder decodeObjectForKey:key];
[self setValue:value forKey:key];
}
}
return self;
}
@end
在此基础上,也可以利用继承,去先执行super的encode方法,在父类的该方法中去处理父类的属性,子类中处理子类的属性更为清晰。
网友评论