- (NSDictionary *)dictionaryRepresentation
{
NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
NSArray *propertyArray = [self getClassPropertys:[self class]];
for(NSString *key in propertyArray)
{
[mutableDict setValue:[self valueForKey:key] forKey:key];
}
return [NSDictionary dictionaryWithDictionary:mutableDict];
}
// 获取某个类自身的所有属性
- (NSArray *)getClassPropertys:(Class)classInstance
{
NSMutableArray *classPropertyDics = [[NSMutableArray alloc] init];
u_int count;
objc_property_t *properties = class_copyPropertyList(classInstance, &count);
for (int i = 0; i < count ; i++) {
const char* propertyName = property_getName(properties[i]);
const char* propertyAttributes = property_getAttributes(properties[i]);
NSString *name = [NSString stringWithUTF8String:propertyName];
NSString *attribute = [NSString stringWithUTF8String:propertyAttributes];
NSString *attributeType = [self parseAttributeType:attribute];
NSMutableDictionary *propertyDic = [[NSMutableDictionary alloc] init];
[propertyDic setObject:attributeType forKey:name];
[classPropertyDics insertObject:propertyDic atIndex:0];
// [classPropertyDics insertObject:name atIndex:0];
}
free(properties);
return (NSArray *)classPropertyDics;
}
- (NSString *)parseAttributeType:(NSString *)Attributes
{
NSRange leftRange = [Attributes rangeOfString:@"\""];
NSString *temp1 = [Attributes substringFromIndex:leftRange.location+1];
NSRange rightRange = [temp1 rangeOfString:@"\""];
NSString *temp2 = [temp1 substringToIndex:rightRange.location];
return temp2;
}
获取某个类自身的所有属性
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 通过 runtime 获取到某个类的所有成员变量: 通过 runtime 获取到某个类的所有属性及其对应的类: 其...
- 在我们的工作中有时候需要获取某些sdk未提供的属性以及方法 ,这个时候应该怎么办呢?可以通过以下方法获取 获取类的...
- 我有一个学姐,她是我见过的,最最最优雅的女生。 她永远妆容精致,衣着得体。熟人的party中,她是笑得最和暖的那...