Runtime获取对象的所有变量和属性
//获得所有变量
- (NSArray *)getAllIvar:(id)object
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
Ivar *ivars = class_copyIvarList([object class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
const char *keyChar = ivar_getName(ivar);
NSString *keyStr = [NSString stringWithCString:keyChar encoding:NSUTF8StringEncoding];
@try {
id valueStr = [object valueForKey:keyStr];
NSDictionary *dic = nil;
if (valueStr) {
dic = @{keyStr : valueStr};
} else {
dic = @{keyStr : @"值为nil"};
}
[array addObject:dic];
}
@catch (NSException *exception) {}
}
return [array copy];
}
//获得所有属性
- (NSArray *)getAllProperty:(id)object
{
NSMutableArray *array = [NSMutableArray array];
unsigned int count;
objc_property_t *propertys = class_copyPropertyList([object class], &count);
for (int i = 0; i < count; i++) {
objc_property_t property = propertys[i];
const char *nameChar = property_getName(property);
NSString *nameStr = [NSString stringWithCString:nameChar encoding:NSUTF8StringEncoding];
[array addObject:nameStr];
}
return [array copy];
}
网友评论