模型的提取Runtime

2016-08-31  本文已影响10人  面朝对象_春暖花开

没写完待续...

//
//  myModle.m
//  字典转模型-runtime,kvc
//
//  Created by yaning Zhu on 16/8/31.
//  Copyright © 2016年 yaning Zhu. All rights reserved.
//

#import "myModle.h"
#import <objc/runtime.h>
@implementation myModle
//1  手动解析,就是每个属性按顺序赋值,笨重切费时
/*2  KVC  ...简单介绍一下KVC,就是key value Codeing的意思,翻译一下就是键值编码,苹果官方为我们能用到的包括NSObject NSArray等常见的类都做了一个叫NSKeyValueCoding的分类,而KVC的操作方法由NSKeyValueCoding协议提供,也就是说我们用到的Objc都是支持KVC的.
 
    再说一下KVC的作用,主要目的就是给对象赋值和取值,当然不单单是给你能看得到的属性,iOS在编译阶段会将一个类的属性及其对应的值都放进内存,可以通过runtime获得到,具体方法下面已经提供,所以KVC能解决官方没有提供接口的某些属性的值,例如UISwitch的颜色等.
 3 小马哥的MJExtension JsonModel等三方
 
*/
- (instancetype)initWithDic:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        //利用KVC解析
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSDictionary * dic = [self getAllPropertiesAndVaules];
        NSLog(@"%@",dic);
        NSArray * arr = [self getAllProperties];
        NSLog(@"%@",arr);
        [self getAllMethods];
    }
    return self;
}
/* 获取对象的所有属性和属性内容 */
- (NSDictionary *)getAllPropertiesAndVaules
{
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    unsigned int outCount, i;
    objc_property_t *properties =class_copyPropertyList([UISwitch 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 = [[UISwitch new] valueForKey:(NSString *)propertyName];
        if (propertyValue) [props setObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
}
/* 获取对象的所有属性 */
- (NSArray *)getAllProperties
{
    u_int count;
    
    objc_property_t *properties  =class_copyPropertyList([UISearchBar 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;
}
/* 获取对象的所有方法 */
-(void)getAllMethods
{
    unsigned int mothCout_f =0;
    Method* mothList_f = class_copyMethodList([self class],&mothCout_f);
    for(int i=0;i<mothCout_f;i++)
    {
        Method temp_f = mothList_f[i];
        IMP imp_f = method_getImplementation(temp_f);
        SEL name_f = method_getName(temp_f);
        const char* name_s =sel_getName(method_getName(temp_f));
        int arguments = method_getNumberOfArguments(temp_f);
        const char* encoding =method_getTypeEncoding(temp_f);
        NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString stringWithUTF8String:name_s],
              arguments,
              [NSString stringWithUTF8String:encoding]);
    }
    free(mothList_f);
}
@end


上一篇下一篇

猜你喜欢

热点阅读