自己写个 字典解析为模型的库


通过Objective-C Runtime函数这个问题得到了很好的解决。

总的来说呢就是通过下面两个函数:

objc_property_t* props = class_copyPropertyList([TestModelclass], &count);

这个可以获得一个类的所有属性的一个数组。

const char * type =property_getAttributes(property);

这个函数可以获得属性的类型。


需要的头文件有


  1. #import <objc/message.h>  
  2. #import <objc/runtime.h>  


大概的代码如下:


  1. unsigned int count;  
  2. objc_property_t* props = class_copyPropertyList([TestModel class], &count);  
  3. for (int i = 0; i < count; i++) {  
  4.     objc_property_t property = props[i];  
  5.     const charchar * name = property_getName(property);  
  6.     NSString *propertyName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];  
  7.     const charchar * type = property_getAttributes(property);  
  8.     NSString *attr = [NSString stringWithCString:type encoding:NSUTF8StringEncoding];  
  9.     NSString * typeString = [NSString stringWithUTF8String:type];  
  10.     NSArray * attributes = [typeString componentsSeparatedByString:@","];  
  11.     NSString * typeAttribute = [attributes objectAtIndex:0];  
  12.     NSString * propertyType = [typeAttribute substringFromIndex:1];  
  13.     const charchar * rawPropertyType = [propertyType UTF8String];  
  14.   
  15.     if (strcmp(rawPropertyType, @encode(float)) == 0) {  
  16.         //it's a float  
  17.     } else if (strcmp(rawPropertyType, @encode(int)) == 0) {  
  18.         //it's an int  
  19.     } else if (strcmp(rawPropertyType, @encode(id)) == 0) {  
  20.         //it's some sort of object  
  21.     } else {  
  22.         // According to Apples Documentation you can determine the corresponding encoding values  
  23.     }  
  24.       
  25.     if ([typeAttribute hasPrefix:@"T@"] && [typeAttribute length] > 1) {  
  26.         NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)];  //turns @"NSDate" into NSDate  
  27.         Class typeClass = NSClassFromString(typeClassName);  
  28.         if (typeClass != nil) {  
  29.             // Here is the corresponding class even for nil values  
  30.         }  
  31.     }  
  32.       
  33. }  
  34. free(props);  


另一种方法是自己去实现获取属性类型的方法


  1. //获取属性名称数组  
  2. - (NSDictionary *)classPropsFor:(Class)klass  
  3. {  
  4.     if (klass == NULL) {  
  5.         return nil;  
  6.     }  
  7.       
  8.     NSMutableDictionary *results = [[NSMutableDictionary alloc] init];  
  9.       
  10.     unsigned int outCount, i;  
  11.     objc_property_t *properties = class_copyPropertyList(klass, &outCount);  
  12.     for (i = 0; i < outCount; i++) {  
  13.         objc_property_t property = properties[i];  
  14.         const charchar *propName = property_getName(property);  
  15.         if(propName) {  
  16.             const charchar *propType = getPropertyType(property);  
  17.             NSString *propertyName = [NSString stringWithUTF8String:propName];  
  18.             NSString *propertyType = [NSString stringWithUTF8String:propType];  
  19.               
  20.             NSLog(@"propertyName %@ propertyType %@", propertyName, propertyType);  
  21.               
  22.             [results setObject:propertyType forKey:propertyName];  
  23.         }  
  24.     }  
  25.     free(properties);  
  26.       
  27.     // returning a copy here to make sure the dictionary is immutable  
  28.     return [NSDictionary dictionaryWithDictionary:results];  
  29. }  
  30. //获取属性的方法  
  31. static const charchar *getPropertyType(objc_property_t property) {  
  32.     const charchar *attributes = property_getAttributes(property);  
  33.     //printf("attributes=%s\n", attributes);  
  34.     char buffer[1 + strlen(attributes)];  
  35.     strcpy(buffer, attributes);  
  36.     charchar *state = buffer, *attribute;  
  37.     while ((attribute = strsep(&state, ",")) != NULL) {  
  38.         if (attribute[0] == 'T' && attribute[1] != '@') {  
  39.             // it's a C primitive type:  
  40.               
  41.             // if you want a list of what will be returned for these primitives, search online for  
  42.             // "objective-c" "Property Attribute Description Examples"  
  43.             // apple docs list plenty of examples of what you get for int "i", long "l", unsigned "I", struct, etc.  
  44.               
  45.             NSString *name = [[NSString alloc] initWithBytes:attribute + 1 length:strlen(attribute) - 1 encoding:NSASCIIStringEncoding];  
  46.             return (const charchar *)[name cStringUsingEncoding:NSASCIIStringEncoding];  
  47.         }  
  48.         else if (attribute[0] == 'T' && attribute[1] == '@' && strlen(attribute) == 2) {  
  49.             // it's an ObjC id type:  
  50.             return "id";  
  51.         }  
  52.         else if (attribute[0] == 'T' && attribute[1] == '@') {  
  53.             // it's another ObjC object type:  
  54.             NSString *name = [[NSString alloc] initWithBytes:attribute + 3 length:strlen(attribute) - 4 encoding:NSASCIIStringEncoding];  
  55.             return (const charchar *)[name cStringUsingEncoding:NSASCIIStringEncoding];  
  56.         }  
  57.     }  
  58.     return "";  
  59. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值