OBJC类扩展之属性字典NSObject+Property
Posted 山里的和尚会玩水
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OBJC类扩展之属性字典NSObject+Property相关的知识,希望对你有一定的参考价值。
#import <Foundation/Foundation.h>#import <objc/runtime.h>
@interface NSObject (Property)
//将对象属性封装到字典,并返回字典
-(NSDictionary *)propertyDictionary;
@end
@implementation NSObject (Property)
-(NSDictionary *)propertyDictionary
{
//创建可变字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned int outCount;
objc_property_t *props = class_copyPropertyList([self class], &outCount);
for(int i=0;i<outCount;i++){
objc_property_t prop = props[i];
NSString *propName = [[NSString alloc]initWithCString:property_getName(prop) encoding:NSUTF8StringEncoding];
id propValue = [self valueForKey:propName];
if(propValue){
[dict setObject:propValue forKey:propName];
}
}
free(props);
return dict;
}
@end
以上是关于OBJC类扩展之属性字典NSObject+Property的主要内容,如果未能解决你的问题,请参考以下文章