IOS NSDictionary扩展映射model(字典--》model;字典==》string)
来源:互联网 发布:淘宝神舟旗舰店 编辑:程序博客网 时间:2024/05/21 16:22
#import <Foundation/Foundation.h>@interface NSDictionary (MS)+(NSString*)dictionary2string:(NSDictionary*)dic;/** * 字典转换成Model * * @param model 要转换的对象 */- (void)toModel:(id)model;@end
#import "NSDictionary+MS.h"@implementation NSDictionary (MS)+(NSString*)dictionary2string:(NSDictionary*)dic{ __block int length = [dic allKeys].count; __block int idx = 0; __block NSMutableString *param = [[NSMutableString alloc] initWithString:@"\{"]; [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { if([obj isKindOfClass:[NSNumber class]]) [param appendFormat:@"\"%@\":%@",(NSString*)key, obj]; else [param appendFormat:@"\"%@\":\"%@\"",(NSString*)key, obj]; idx++; if (idx == length) { [param appendString:@"}"]; *stop = YES; } else { [param appendString:@","]; } }]; return param;}/** * 字典转换成Model * * @param model 要转换的对象 */- (void)toModel:(id)model{ if (self.count > 0) { NSEnumerator *keyEnumer = [self keyEnumerator]; for (id key in keyEnumer) { NSString *propertyName = key; id propertyValue = [self valueForKey:propertyName]; if (propertyValue == nil || propertyValue == [NSNull null]) continue; if ([propertyValue isKindOfClass:[NSDictionary class]]) { Class typeClass = typeNameWithClass([model class], propertyName); if (![typeClass isSubclassOfClass:[NSDictionary class]]) { id obj = [[typeClass alloc] init]; [propertyValue toModel:obj]; propertyValue = obj; } } else if ([propertyValue isKindOfClass:[NSArray class]]) { if ([model respondsToSelector:@selector(classAtInsideOfObjectWithProperty:)]) { Class objClass = [model classAtInsideOfObjectWithProperty:propertyName]; if (objClass != Nil) { NSMutableArray *tempDict = [[NSMutableArray alloc] initWithCapacity:[propertyValue count]]; for (NSDictionary *dict in propertyValue) { id obj = [[objClass alloc] init]; [dict toModel:obj]; [tempDict addObject:obj]; } if (tempDict.count > 0) propertyValue = tempDict; } } } NSString *propertySetMethod = [NSString stringWithFormat:@"set%@%@:", [[propertyName substringToIndex:1] capitalizedString] ,[propertyName substringFromIndex:1]]; SEL selector = NSSelectorFromString(propertySetMethod); if ([model respondsToSelector:selector]) { [model setValue:propertyValue forKey:propertyName]; } } }}/************************************附加方法************************************/#define kAttributeType "T"//变量类型#define kAttributeVariable "V"//变量名称#define kAttributeSetter "S"//set方法名称#define kAttributeGetter "G"//get方法名称/** * 获取类中属性的类型 * * @param cls 类结构 * @param varName 属性名称 * * @return 类型 */Class typeNameWithClass(Class cls, NSString *varName){ if (varName.length <= 0) return nil; unsigned int outCount, i; NSString *result; //从属性找 while (cls != [NSObject class]) { objc_property_t *propertys = class_copyPropertyList(cls, &outCount); for (i = 0; i < outCount; ++i) { objc_property_t property = propertys[i]; unsigned int outAttribute; objc_property_attribute_t *attributes= property_copyAttributeList(property, &outAttribute); NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; NSString *variableName, *getMethodName, *setMethodName, *typeName; for (int j = 0; j < outAttribute; ++j) { objc_property_attribute_t attribute = attributes[j]; NSString *value = [NSString stringWithFormat:@"%s", attribute.value]; if (strcmp(attribute.name, kAttributeType) == 0) typeName = [[value stringByReplacingOccurrencesOfString:@"@\"" withString:@""] stringByReplacingOccurrencesOfString:@"\"" withString:@""]; else if (strcmp(attribute.name, kAttributeVariable) == 0) variableName = value; else if (strcmp(attribute.name, kAttributeSetter) == 0) setMethodName = value; else if (strcmp(attribute.name, kAttributeGetter) == 0) getMethodName = value; } if ((propertyName.length > 0 && [propertyName compare:varName options:NSCaseInsensitiveSearch] == NSOrderedSame) || (variableName.length > 0 && [variableName compare:varName options:NSCaseInsensitiveSearch] == NSOrderedSame) || (getMethodName.length > 0 && [getMethodName compare:varName options:NSCaseInsensitiveSearch] == NSOrderedSame)) { result = typeName; } else { NSString *propertySetMethod = [NSString stringWithFormat:@"set%@%@:", [[varName substringToIndex:1] capitalizedString] ,[varName substringFromIndex:1]]; if (setMethodName.length > 0 && [setMethodName compare:propertySetMethod options:NSCaseInsensitiveSearch] == NSOrderedSame) result = typeName; } if (attributes) free(attributes); if (result.length > 0) break; } if (propertys) free(propertys); if (result.length > 0) break; cls = class_getSuperclass(cls); } return result.length > 0 ? NSClassFromString(result) : Nil;}@end
使用:
#import "NSDictionary+MS.h"
-(id)initWithNotifyInfo:(NSDictionary *)dic
{
if (self=[superinit]) {
[dictoModel:self];
}
return self;
}
0 0
- IOS NSDictionary扩展映射model(字典--》model;字典==》string)
- iOS Mantle解析框架简单的使用入门(字典映射model)
- IOS 字典快速转换为Model 模型
- iOS NSDictionary和Model
- 字典与model
- 字典与Model类
- 自定义model 转出 字典
- 将model转化为字典
- IOS中 NSDictionary(字典)排序
- 漫步IOS--字典类(NSDictionary)
- iOS开发NSDictionary字典介绍
- iOS NSDictionary(字典)~实现原理
- IOS反射,swift反射,基础 MODEL转字典,字典转MODEL,根据下面的自己进行拓展
- iOS--字典数组排序(每个数组里面都是一个model)NSSortDescriptor
- NSDictionary------字典
- 字典:NSDictionary
- 字典NSDictionary
- 字典NSDictionary
- 大数据实践应用分享
- hdu 2007 平方和与立方和
- C风格字符串和C++string类对比 使用实例
- 如何转换成Ogg和MPEG4格式
- tiny210(s5pv210)移植u-boot(基于 2014.4 版本)——移植u-boot.bin(修改显示信息)
- IOS NSDictionary扩展映射model(字典--》model;字典==》string)
- 拿起笔来做刀枪 · 之四 再造一个struts
- 拿起笔来做刀枪 · 之五 再造一个lucene
- tiny210(s5pv210)移植u-boot(基于 2014.4 版本)——移植u-boot.bin(打印串口控制台)
- 集成 Tomcat 插件到 Eclipse 的过程
- Travis Karr: 我们就是不断在改变,这是我们的宿命(二)
- 自定义背景和显示时间的Toast
- 鸡肋的JdbcRDD
- startActivityForResult用法详解