前言
在iOS项目中,用户信息等不大不小的数据我们一般会归档到沙盒文件中,但当需要归档的属性较多时,或后续添加、减少属性时,都要操作encodeWithCoder:
initWithCoder:
两个方法。
在你的项目中,归档的代码可能是这样的:
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:_uuid forKey:@"uuid"];
[encoder encodeObject:self.token forKey:@"token"];
[encoder encodeObject:_username forKey:@"username"];
[encoder encodeObject:_avatar forKey:@"avatar"];
[encoder encodeObject:_realname forKey:@"realname"];
[encoder encodeObject:_gender forKey:@"gender"];
[encoder encodeObject:_school forKey:@"school"];
[encoder encodeObject:_graduateTime forKey:@"graduateTime"];
[encoder encodeObject:_examType forKey:@"examType"];
[encoder encodeObject:_examArea forKey:@"examArea"];
[encoder encodeObject:_nickname forKey:@"nickname"];
[encoder encodeObject:_studyPhase forKey:@"studyPhase"];
[encoder encodeObject:_subject forKey:@"subject"];
[encoder encodeObject:_consignee forKey:@"consignee"];
[encoder encodeObject:_mobileno forKey:@"mobileno"];
[encoder encodeObject:_province forKey:@"province"];
[encoder encodeObject:_city forKey:@"city"];
[encoder encodeObject:_area forKey:@"area"];
[encoder encodeObject:_street forKey:@"street"];
[encoder encodeObject:_gold forKey:@"gold"];
}
虽然com+c
com+v
很简单,可是看着还是比较冗余。
runtime几个函数介绍
Ivar *class_copyIvarList(Class cls, unsigned int *outCount);
作用:拷贝一个类的实例变量(不论是.h还是.m文件)
-
参数:
-
cls
:指定的Class对象 -
outCount
:int型的指针,实例变量的个数,函数内部会对其进行赋值操作
-
返回值:返回一个指针数组,元素是Ivar类型。可以对其进行遍历
const char *ivar_getName(Ivar v);
- 作用:获取实例变量的名称
- 参数:
v
:Ivar类型的实例变量 - 返回值:C的字符串,可以通过
[NSString stringWithUTF8String:cString]
转为OC字符串
runtime函数演示
先创建一个Person类,并分别在.h文件
类extension
类implementation
三个地方声明几个实例变量,如下:
// Person.h
@interface Person : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, assign) BOOL gender;
@end
// Person.m
@interface Person ()
@property (nonatomic, assign) CGFloat height;
@end
@implementation Person {
NSString *_lastName;
}
@end
我们在VC
中导入<objc/runtime.h>
,并使用以上介绍的函数书写以下代码,获取Person
类的实例变量数及名称
- (void)viewDidLoad {
[super viewDidLoad];
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([Person class], &count);
NSLog(@"%d", count);
for (int i=0; i<count; i++) {
Ivar ivar = ivars[i];
const char *name = ivar_getName(ivar);
NSString *key = [NSString stringWithUTF8String:name];
NSLog(@"%@", key);
}
}
先自己考虑下打印结果
打印如下:
2017-09-29 15:16:55.372 Demo[2981:241191] 5
2017-09-29 15:16:55.372 Demo[2981:241191] _lastName
2017-09-29 15:16:55.373 Demo[2981:241191] _gender
2017-09-29 15:16:55.373 Demo[2981:241191] _firstName
2017-09-29 15:16:55.373 Demo[2981:241191] _age
2017-09-29 15:16:55.373 Demo[2981:241191] _height
完全符合预想。
KVC
只是简单的setValue:forKey:
valueForKey:
操作,不再赘述。
归档解档实现
通过以上介绍,相信大家也猜到怎么实现了。
原理:
归档时,通过以上两个函数获取到所有实例变量名称,再通过KVC
获取到对应的value
,进行归档。解档时,通过获取到的名称 使用KVC
进行赋值。
代码实现:
- (instancetype)initWithCoder:(NSCoder *)decoder {
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 = [decoder decodeObjectForKey:key];
[self setValue:value forKey:key];
}
free(ivars);
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
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 = [self valueForKey:key];
[encoder encodeObject:value forKey:key];
}
free(ivars);
}
不要忘记释放堆内存ivars,不然会造成内存泄露。
这样代码看起来就不那么冗余了,而且不管以后增加/减少属性,都不需要改动encodeWithCoder:
initWithCoder:
的代码了。