您的位置:首页 > 移动开发 > Objective-C

第02天实战技术(03):Runtime(字典转模型,一级转换)

2017-03-25 00:00 453 查看
#####一、使用KVC进行字典转模型的弊端

功能不够强大
比如模型里面有字典(我们有需要通过字典转模型),但是KVC是不知道的,
KVC也是可以做的,需要单独处理

.h
@interface StatusModel : NSObject
@property(nonatomic, strong) NSString * source;
@property(nonatomic, assign) NSInteger  reposts_count;
@property(nonatomic, strong) NSArray * pic_urls;
@property(nonatomic, strong) NSString * created_at;
@property(nonatomic, assign) BOOL  isA;
@property(nonatomic, assign) NSInteger  attitudes_count;
@property(nonatomic, strong) NSString * idstr;
@property(nonatomic, strong) NSString * text;
@property(nonatomic, assign) NSInteger  comments_count;
@property(nonatomic, strong) User * user;
+ (instancetype)modelWithDict:(NSDictionary *)dict;
@end

.m
#pragma 模型里面包含模型 使用KVC处理,需要单独做处理
- (void)setUser:(NSDictionary *)user
{
_user =
}

#####二、 runtime的小知识点

1.runtime字典转模型的一些方法

参数
// <#__unsafe_unretained Class cls#> 获取哪个类的成员变量
// <#unsigned int *outCount#> 成员变量的总数

>>>>
// 获取类里面的所有方法 (copy的好处就是 避免修改系统里面的东西)
// class_copyMethodList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>)

// 获取类里面的属性
// Property : 属性
// class_copyPropertyList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>) 比较少用这个

// 获取类里面的成员属性
// Ivar : 成员变量 以下划线开头
// class_copyIvarList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>) *** 开发者最常用这个


2.带指针得函数

#pragma mark 函数
// 带有指针的函数
void test (int *count)
{
*count = 3;
}


3.runtime获取成员变量的数组需要用一个指针来获取

/*
OBJC_EXPORT Ivar *class_copyIvarList(Class cls, unsigned int *outCount)
OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0);
Ivar * 返回一个数组

例子:
int a = 2;
int b = 3;
int c = 4;

int arr[] = {a,b,c};
int *p = arr;
NSLog(@"%d %d",p[0],p[1]);
(int *) p = 0x00007fff5de868ec
*/




4.
成员变量
属性
的区别

#pragma mark 成员变量 与 属性的 区别
/**
成员变量 是以 _开头的
{
int _a;
}

@property(nonatomic, assign) NSInteger  reposts_count; // reposts_count 属性名字  _reposts_count 成员变量
为什么要用 Ivar 因为我们使用成员变量不会漏掉属性,但是我们用属性 可能会漏掉成员变量

*/

#####三、(重点)使用RunTime进行字典转模型

模型是一个对象,需要为NSObject添加一个分类(用来字典转模型的)

使用方式

NSObject+Model(为
字典转模型
NSObject
扩充一个
分类
)

// 本质 : 创建谁的对象
+ (instancetype)modelWithDictionary:(NSDictionary *)dict
{
// 使用runtime实现 字典转模型
// 1.获取对象
// 2. 把字典中所有的值 给模型赋值 (正常的想法)

id objc = [[self alloc]init];

// runtime : 根据模型中属性,去字典中取出对应的value给模型属性赋值 (runtime想法)
// 1.获取模型中所有成员变量 key
// <#__unsafe_unretained Class cls#> 获取哪个类的成员变量
// <#unsigned int *outCount#> 成员变量的总数
unsigned count = 0;

// 获取成员变量数组 (class_copyIvarList)
Ivar *ivarList =  class_copyIvarList(self, &count);

//    Ivar ivar = ivarList[0]; // 取出是成员变量
//    // 获取成员变量名字
//    NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 并且将C的字符串 转换成 OC的名字

// 遍历所有的成员变量
for (int i = 0 ; i<count; i++) {
Ivar ivar = ivarList[i]; // 取出是成员变量
// 获取成员变量名字
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)]; // 并且将C的字符串 转换成 OC的名字
NSLog(@"%@",ivarName);

// 获取key
// 裁剪第1位
NSString *key = [ivarName substringFromIndex:1];
//        NSLog(@"key : %@",key);

// 2.根据属性名去字典中查找 value
id value = dict[key];

// 3.给模型中属性赋值 KVC
// 判断有值才进行赋值
if (value) {
[objc setValue:value forKey:key];
}

}

//    test(&count);
NSLog(@"%i",count);

return objc;
}

#import "ViewController.h"
#import "NSDictionary+Property.h"
#import "StatusModel.h"
#import "NSObject+Model.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// 文件的全路径
// 1.获取文件的全路径
NSString *path = [[NSBundle mainBundle]pathForResource:@"status.plist" ofType:nil];
// 2.生成了一个字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@" -- %@",dict);

// 设置模型, 创建属性代码 => dict
// [dict createPropertyCode];

// 3.字典转模型
StatusModel *model = [StatusModel modelWithDictionary:dict];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Objective-C RunTime