runtime运用 -从一个模型中找出所有属性赋值给另外一个模型
2016-06-02 本文已影响314人
65067d1326a2
-(talkuser*)changeModle:(id)model
{
unsigned int count;
objc_property_t *properties=class_copyPropertyList([model class], &count);
talkuser *backModel=[[talkuser alloc]init];
for(int i =0; i < count; i++) {
objc_property_t property = properties[i];
//获取属性的名称C语言字符串
const char *cName =property_getName(property);
//转换为Objective C字符串
NSString *name = [NSString stringWithCString:cName encoding:NSUTF8StringEncoding];
id value= [model valueForKey:name];
[backModel setValue:value forKey:name];
}
free(propertys);
returnbackModel;
}