打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS培训通过runtime获取类属性和类方法

iOS培训通过runtime获取类属性和类方法

//  NSObject+DateBaseModel.h
//  LightApp
//
//  Created by malong on 14/11/15.
//  Copyright (c) 2014
malong. All rights reserved.
//


/*
runtime
是什么:运行时刻是指一个程序在运行(或者在被执行)的状态。Runtime类封装了运行时的环境。它属于C语言库, 有很多底层的C语言API
要点:1、不能实例化一个Runtime对象
      2
、可以通过 getRuntime 方法获取当前Runtime运行时对象的引用

runtime
可以干什么?
runtime
是属于OC的底层, 可以进行一些非常底层的操作(OC是无法现实的, 不好实现)
在程序运行过程中, 动态创建一个类(比如KVO的底层实现)、动态地为某个类添加属性\方法, 修改属性值\方法
遍历一个类的所有成员变量(属性)\所有方法



典型例子:KVO的底层实现

*/
#import <Foundation/Foundation.h>


/**
* @brief
数据库模型类目
*/

@interface NSObject (DateBaseModel)

/**
* @brief
属性列表
*/
- (NSDictionary *)propertiesDic;

/**
* @brief
属性列表名数组
*/
- (NSMutableArray*)propertyNames;


/**
* @brief
属性对应值数组
*/
- (NSMutableArray*)propertyVaules;


/**
* @brief
获取对象的所有方法信息
*/
-(NSArray *)mothodLists;


- (void) setFlashColor:(UIColor *) flashColor;
- (UIColor *) getFlashColor;


@end
[/mw_shl_code]



[mw_shl_code=objc,true]//
//  NSObject+DateBaseModel.m
//  LightApp
//
//  Created by malong on 14/11/15.
//  Copyright (c) 2014
malong. All rights reserved.
//

#import "NSObject+DateBaseModel.h"
#import <objc/runtime.h>

@implementation NSObject (DateBaseModel)

- (NSDictionary *)propertiesDic {

    NSMutableDictionary *props =[NSMutableDictionary dictionary];
    
    unsigned int outCount, i;
    
    objc_property_t * properties =class_copyPropertyList([self class], &outCount);
    
    for (i = 0; i < outCount; i++){
       objc_property_t property = properties;
        
        NSString *propertyName = [[NSStringalloc] initWithCString:property_getName(property)encoding:NSUTF8StringEncoding];
        
        id propertyValue = [self valueForKey:(NSString*)propertyName];
        if (propertyValue) [propssetObject:propertyValue forKey:propertyName];
    }
    free(properties);
    return props;
    
//    return [self dictionaryWithValuesForKeys:[self propertyNames]];
}


- (NSMutableArray*)propertyNames{
    
    unsigned int outCount, i;
    
    objc_property_t * properties = class_copyPropertyList([selfclass], &outCount);
    
    NSMutableArray * propertyNames = [NSMutableArrayarrayWithCapacity:outCount];
    
    for (i = 0; i < outCount; i++) {
        
        objc_property_t property = properties;
        
        NSString *propertyName = [[NSStringalloc] initWithCString:property_getName(property)encoding:NSUTF8StringEncoding];
        
        [propertyNames addObject:propertyName];
        
    }
    
    free(properties);
    return propertyNames;
}



- (NSMutableArray *)propertyVaules{
    
    NSMutableArray * propertyNames = [self propertyNames];
    NSMutableArray * propertyVaules = [NSMutableArrayarrayWithCapacity:propertyNames.count];
    
    for (int i = 0; i<propertyNames.count; i++) {
        
        id propertyValue = [selfvalueForKey:[propertyNames objectAtIndex:i]];
        
        if (nil != propertyValue) {
            [propertyVaulesaddObject:propertyValue];
        }
    }
    
    return propertyVaules;
//    return [[self propertiesDic] allValues];
}


/**
* @brief
获取对象的所有方法信息
*/
-(NSArray *)mothodLists
{
    unsigned int mothCout =0;
    
    //
获取方法列表
    Method* methodList = class_copyMethodList([self class],&mothCout);
    
    //
创建存储方法信息的数组
    NSMutableArray * methodlists = [NSMutableArray array];
    
    //
遍历数组,提取每个方法的信息
    for(int i=0;i<mothCout;i++)
    {
        //
获取方法
        Method method = methodList;
//        IMP imp_f =method_getImplementation(temp_f);
//        SEL name_f = method_getName(temp_f);
        
        //
创建方法信息字典
        NSMutableDictionary * methodInfo =[NSMutableDictionary dictionary];
        
        
        //
获取方法名
        const char * name=sel_getName(method_getName(method));
        NSString * methodName = [NSStringstringWithUTF8String:name];
        [methodInfo setValue:methodNameforKey:@"methodName"];
        
        
        //
获取方法返回值类型
        const char * returnType =method_copyReturnType(method);
        NSString * returnTypeString = [NSStringstringWithUTF8String:returnType];
        [methodInfo setValue:returnTypeStringforKey:@"returnTypeString"];

        
        //
获取方法参数个数、每个参数的类型
        int arguments =method_getNumberOfArguments(method);
        if (arguments) {
            //
创建存储参数类型名的数组
            NSMutableArray *methodArgumentTypes = [NSMutableArray array];
            
            for (int j = 0;j<arguments; j++) {
                //
获取每个参数的类型
                charargumentType[256];
                
               method_getArgumentType(method, j, argumentType, 256);
                
                NSString *argumentTypeName = [NSString stringWithUTF8String:argumentType];
               [methodArgumentTypes addObject:argumentTypeName];
            }
            
            [methodInfosetObject:methodArgumentTypes forKey:@"methodArgumentTypes"];
        }
        
        
        //
获取方法编码格式
        const char* encoding =method_getTypeEncoding(method);
        NSString * encodingName = [NSString stringWithUTF8String:encoding];
        [methodInfo setValue:encodingNameforKey:@"encodingName"];

        
        
        DLog(@"
方法名:%@,参数个数:%d,编码方式:%@",methodName,
              arguments,
              [NSStringstringWithUTF8String:encoding]);
        
        [methodlists addObject:methodInfo];
        
    }
    
    free(methodList);
    
    return methodlists;
}

static char flashColorKey;

- (void) setFlashColor:(UIColor *) flashColor{
    objc_setAssociatedObject(self, &flashColorKey, flashColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *) getFlashColor{
    return objc_getAssociatedObject(self, &flashColorKey);
}



@end


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iOS:学习runtime的理解和心得
iOS模式详解runtime面试工作
(译)Objective
新手也看得懂的 iOS Runtime 教程
iOS 万能跳转界面方法 (runtime实用篇一)
Swift/Objc的Runtime(运行时)机制
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服