标题:
用runtime遍历用户属性的方法与传统方法的比较 更新
[打印本页]
作者:
look_w
时间:
2019-2-19 17:32
标题:
用runtime遍历用户属性的方法与传统方法的比较 更新
runtime遍历属性
/*
对象归档解档改为运行时进行:
*/
#pragma mark - 对象归档
- (void)encodeWithCoder
NSCoder *)aCoder {
unsigned int count = 0 ;
Ivar *ivars = class_copyIvarList([User class], &count) ;
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i] ;
const char *propertyName = ivar_getName(ivar) ;
NSString *key = [[NSString alloc] initWithUTF8String:propertyName] ;
[aCoder encodeObject:[self valueForKey:key] forKey:key] ;
}
}
#pragma mark - 对象解档
- (instancetype)initWithCoder
NSCoder *)aDecoder {
self = [super init] ;
if (self) {
unsigned int count = 0 ;
Ivar *ivars = class_copyIvarList([User class], &count) ;
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i] ;
const char *propertyName = ivar_getName(ivar) ;
NSString *key = [[NSString alloc] initWithUTF8String:propertyName] ;
id value = [aDecoder decodeObjectForKey:key] ;
[self setValue:value forKey:key] ;
}
}
return self ;
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0