快速归档解档
2016-04-07 17:20
232 查看
创建一个BaseModel类,所有的对象都继承这个BaseModel就可以了。附上BaseModel代码
#import "BaseModel.h"
#import <objc/runtime.h>
@implementation BaseModel
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super
init]) {
Class c = self.class;
while (c && c != [NSObject
class]) {
unsigned
int count = 0;
Ivar *ivars =
class_copyIvarList(c, &count);
for (int i =
0; i < count; i++) {
NSString *key = [NSString
stringWithUTF8String:ivar_getName(ivars[i])];
id value = [aDecoder
decodeObjectForKey:key];
[self
setValue:value forKey:key];
}
c = [c superclass];
free(ivars);
}
}
return
self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
Class c = self.class;
while (c && c != [NSObject
class]) {
unsigned int count =
0;
Ivar *ivars =
class_copyIvarList(c, &count);
for (int i =
0; i < count; i++) {
Ivar ivar = ivars[i];
NSString *key = [NSString
stringWithUTF8String:ivar_getName(ivar)];
id value = [self
valueForKey:key];
[aCoder encodeObject:value
forKey:key];
}
c = [c superclass];
free(ivars);
}
}
@end
#import "BaseModel.h"
#import <objc/runtime.h>
@implementation BaseModel
- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super
init]) {
Class c = self.class;
while (c && c != [NSObject
class]) {
unsigned
int count = 0;
Ivar *ivars =
class_copyIvarList(c, &count);
for (int i =
0; i < count; i++) {
NSString *key = [NSString
stringWithUTF8String:ivar_getName(ivars[i])];
id value = [aDecoder
decodeObjectForKey:key];
[self
setValue:value forKey:key];
}
c = [c superclass];
free(ivars);
}
}
return
self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
Class c = self.class;
while (c && c != [NSObject
class]) {
unsigned int count =
0;
Ivar *ivars =
class_copyIvarList(c, &count);
for (int i =
0; i < count; i++) {
Ivar ivar = ivars[i];
NSString *key = [NSString
stringWithUTF8String:ivar_getName(ivar)];
id value = [self
valueForKey:key];
[aCoder encodeObject:value
forKey:key];
}
c = [c superclass];
free(ivars);
}
}
@end
相关文章推荐
- Android之旅---广播(BroadCast)
- LintCode-乱序字符串
- 网页页面蒙版实现
- Yii的常用URL和渲染方法
- 帧动画
- centos7 无法启动网卡
- 源码安装Redis
- php生成excel方法二
- 随机十个数求和5
- CentOS 6 64bit环境一键安装VNC桌面环境教程
- 3160: 万径人踪灭|FFT|manacher
- Codeforces 588C Duff and Weight Lifting 【数学】
- 如何用SQL命令查看Mysql数据库大小
- Extjs问题——windows包容多组件
- Codeforces 588B Duff in Love 【数学】
- weblogic调优总结
- Junit4中怎样随心所欲的执行Test(一)
- swift常用宏/获取设备信息
- 剑指offer 18
- 【堆/排序】堆排序的两种建堆方法