反射 dic转换成对象

来源:互联网 发布:淘宝死号怎样恢复 编辑:程序博客网 时间:2024/06/10 22:34

+(MicroPage*)creatPage:(NSMutableDictionary*)dic
{
    //反射 对象赋值
    
    MicroPage *m = [[[MicroPage alloc] init] autorelease];
    unsigned int outCount;
    objc_property_t *properties = class_copyPropertyList([m class], &outCount);
    for (int i=0; i<outCount; i++) {
        const char *pName = property_getName(properties[i]);
        NSString *propertyName = [NSString stringWithCString:pName encoding:NSUTF8StringEncoding];
        id value = [dic objectForKey:propertyName];
        [m setValue:value forKey:propertyName];
    }
    return m;
}


原创粉丝点击