用runtime遍历用户属性的方法与传统方法的比较 更新
- UID
- 1066743
|
用runtime遍历用户属性的方法与传统方法的比较 更新
runtime遍历属性
/*
对象归档解档改为运行时进行:
*/
#pragma mark - 对象归档
- (void)encodeWithCoderNSCoder *)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)initWithCoderNSCoder *)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 ;
} |
|
|
|
|
|