美文网首页
model 转换

model 转换

作者: 张家杰仔 | 来源:发表于2017-10-19 15:19 被阅读10次
说明:该博客是转载的,由于是记了很久的笔记现在才放到博客上,原作者的博客地址找不到了,忘谅解

#import <objc/runtime.h>

//获取对象的所有属性
- (NSArray *)getAllProperties
{
    u_int count;
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i<count; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        [propertiesArray addObject: [NSString stringWithUTF8String: propertyName]];
    }
    free(properties);
    return propertiesArray;
}

//Model 到字典
- (NSDictionary *)properties_aps
{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    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];
        if (propertyValue) {
            [props setObject:propertyValue forKey:propertyName];
        }else{
          [props setObject:@“” forKey:propertyName];
      }
    }
    free(properties);
    return props;
}

相关文章

网友评论

      本文标题:model 转换

      本文链接:https://www.haomeiwen.com/subject/llpuyxtx.html