- (NSMutableDictionary *)createDictionayFromModelProperties
{
NSMutableDictionary *propsDic = [NSMutableDictionary dictionary];
unsigned int outCount, i;
// class:获取哪个类的成员属性列表
// count:成员属性总数
// 拷贝属性列表
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (i = 0; i<outCount; i++) {
objc_property_t property = properties[i];
const char* char_f = property_getName(property);
// 属性名
NSString *propertyName = [NSString stringWithUTF8String:char_f];
// 属性值
id propertyValue = [self valueForKey:(NSString *)propertyName];
// 设置KeyValues
if (propertyValue) [propsDic setObject:propertyValue forKey:propertyName];
}
// 需手动释放 不受ARC约束
free(properties);
return propsDic;
}
iOS根据模型属性生成字典 (字典转模型,使用Runtime)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 名字好长。。。。希望大家能理解。 下面直接贴代码,创建一个继承自NSObject的基本model 这个数组是用来装...
- 工具github地址:https://github.com/linhaosunny/LSXExtension ...
- 1 动态添加属性 若想给系统的类添加属性,可以采用Runtime的方法,比如:给系统的NSObject类添加一个n...
- 前言:一般情况下我们拿到数据都会有一个模型类,这时候要是属性少还好,要是属性多的话,那就苦逼了,能不能不用写这些垃...