1、获取类的属性方法
OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
2、获得属性的值
OBJC_EXPORT const char *property_getName(objc_property_t property)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
例子:(把某个类属性转换成字典)
- (NSDictionary *)getObjectData:(id)obj
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
unsigned int propsCount;
objc_property_t *props = class_copyPropertyList([obj class], &propsCount); //获得属性列表
for (int i = 0; i < propsCount; i++) {
objc_property_t prop = props[i];
NSString *propName = [NSString stringWithUTF8String:property_getName(prop)]; //获得属性的名称
id value = [obj valueForKey:propName]; //kvc读值
if (value == nil) {
value = [NSNull null];
}
else {
value = [self getObjectInternal:value]; //自定义处理数组,字典,其他类
}
[dic setObject:value forKey:propName];
}
return dic;
}
网友评论