iOS分享世界MG的分类和继承iOS学习开发

RunTime与快速字典转模型(KVC)

2016-08-30  本文已影响169人  Mg明明就是你

方式一:快速字典转模型(KVC)

.h文件

#################### .h文件 ####################
//  NSObject+Log.h
//  05-字典转模型(KVC)
//  Created by ming on 16/1/12.
//  Copyright © 2016年 ming. All rights reserved.

#import <Foundation/Foundation.h>

@interface NSObject (Log)

+ (void)resolveDict:(NSDictionary *)dict;
@end```


# .m文件 

#################### .m文件 ####################
// NSObject+Log.m
// 05-字典转模型(KVC)
// Created by ming on 16/1/12.
// Copyright © 2016年 ming. All rights reserved.

import "NSObject+Log.h"

@implementation NSObject (Log)

// 自动打印属性字符串

}
@end


#模型

#################### Status.h ####################
// Status.h
// 05-字典转模型(KVC)
// Created by ming on 16/1/12.
// Copyright © 2016年 ming. All rights reserved.

import <Foundation/Foundation.h>

@interface Status : NSObject
/** 字典转模型 */

#################### Status.m ####################
// Status.m
// 05-字典转模型(KVC)

import "Status.h"

@implementation Status

/** 字典转模型 */

@end




#控制器

#################### .控制器文件 ####################
// ViewController.m
// 05-字典转模型(KVC)

import "ViewController.h"

import "NSObject+Log.h"

import "Status.h"

@interface ViewController ()
/** <#注释#> */
@property (nonatomic,strong) NSMutableArray *statuses;
@end

@implementation ViewController

@end```



快速转化成了属性.png


方式二:RunTime

  • 思路:利用运行时,遍历模型中所有属性,根据模型的属性名,去字典中查找key,取出对应的值,给模型的属性赋值。
#import <Foundation/Foundation.h>
@protocol ModelDelegate <NSObject>@optional
// 提供一个协议,只要准备这个协议的类,都能把数组中的字典转模型、、用在三级数组转换
+ (NSDictionary *)arrayContainModelClass;
@end

@interface NSObject (Item)
// 字典转模型
+ (instancetype)objectWithDict:(NSDictionary *)dict;
@end```

import "NSObject+Item.h"

import <objc/message.h>

@implementation NSObject (Item)

/** 把字典中所有value给模型中属性赋值,

##############################################二级转换###################################################
// 二级转换,字典中还有字典,也需要把对应字典转换成模型
// // 判断下value,是不是字典
if ([value isKindOfClass:[NSDictionary class]] && ![ivarType containsString:@"NS"]) {
// 是字典对象,并且属性名对应类型是自定义类型
// user User
// 处理类型字符串 @"User" -> User
ivarType = [ivarType stringByReplacingOccurrencesOfString:@"@" withString:@""];
ivarType = [ivarType stringByReplacingOccurrencesOfString:@""" withString:@""];
// 自定义对象,并且值是字典
// value:user字典 -> User模型
// 获取模型(user)类对象

       Class modalClass = NSClassFromString(ivarType); 
       // 字典转模型
       if (modalClass) { 
           // 字典转模型  
           user value = [modalClass objectWithDict:value];  
        } 

       // 字典,user 
       // NSLog(@"%@",key);  
    }  

##############################################三级转换###################################################
// 三级转换:NSArray中也是字典,把数组中的字典转换成模型.
// 判断值是否是数组
if ([value isKindOfClass:[NSArray class]]) {
// 判断对应类有没有实现字典数组转模型数组的协议
if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
// 转换成id类型,就能调用任何对象的方法
id idSelf = self;
// 获取数组中字典对应的模型
NSString *type = [idSelf arrayContainModelClass][key];
// 生成模型
Class classModel = NSClassFromString(type);
NSMutableArray *arrM = [NSMutableArray array];

          // 遍历字典数组,生成模型数组 
          for (NSDictionary *dict in value) { 
            // 字典转模型 
            id model = [classModel objectWithDict:dict]; 
            [arrM addObject:model]; 
          } 
          // 把模型数组赋值给
          value value = arrM; 
       } 
    } 
    // 2.5 KVC字典转模型 
    if (value) {  
       [objc setValue:value forKey:key];  
   }  
}  
// 返回对象
return objc;}@end

}

***

######模型代码

import <Foundation/Foundation.h>

import "NSObject+Item.h"

@class User;@interface Status :NSObject <ModelDelegate>

@property (nonatomic, strong) NSString *source;
@property (nonatomic, assign) int reposts_count;
@property (nonatomic, strong) NSArray *pic_urls;
@property (nonatomic, strong) NSString *created_at;
@property (nonatomic, assign) int attitudes_count;
@property (nonatomic, strong) NSString *idstr;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) int comments_count;
@property (nonatomic, strong) User *user;

@end```

#import "Status.h"
@implementation Status
+ (NSDictionary *)arrayContainModelClass { 
    return @{ @"pic_urls" : @"Picture" };
}
@end```
上一篇 下一篇

猜你喜欢

热点阅读