zoukankan      html  css  js  c++  java
  • 获取类属性 变量 方法

    #import <Foundation/Foundation.h>
    typedef NSArray<NSString *> GLStringArray;
    typedef NSMutableArray<NSString *> GLStringMutableArray;
    //@compatibility_alias GLStringMutableArray NSMutableArray<NSString *>;
    
    @interface NSObject (GL)
    
    //变量
    @property (nonatomic, class,readonly) GLStringArray * instanceVarNames;
    //方法
    @property (nonatomic, class,readonly) GLStringArray * instanceMethodNames;
    @property (nonatomic, class,readonly) GLStringArray * classMethodNames;
    //属性
    @property (nonatomic, class,readonly) GLStringArray * propertyNames;
    
    @end
    
    #import "NSObject+GL.h"
    #import <objc/runtime.h>
    
    @implementation NSObject (GL)
    
    + (GLStringArray *)instanceVarNames{
        unsigned int m = 0;
        Ivar * ivarList = class_copyIvarList([self class], &m);
        GLStringMutableArray * array = [[GLStringMutableArray alloc] init];
        for (int i = 0; i < m; i ++) {
            Ivar ivar = ivarList[i];
            const char * name = ivar_getName(ivar);
            NSString * str = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
            if (str) {
                [array addObject:str];
            }
        }
        return array.copy;
    }
    
    + (GLStringArray *)instanceMethodNames{
        return [self gl_methodNamesWithClass:[self class]];
    }
    
    + (GLStringArray *)classMethodNames{
        return [self gl_methodNamesWithClass:object_getClass(self)];
    }
    
    + (GLStringArray *)propertyNames{
        unsigned int m = 0;
        objc_property_t * propertyList = class_copyPropertyList([self class], &m);
        GLStringMutableArray * array = [[GLStringMutableArray alloc] init];
        for (int i = 0; i < m; i ++) {
            objc_property_t property = propertyList[i];
            const char * name = property_getName(property);
            NSString * str = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
            if (str) {
                [array addObject:str];
            }
        }
        return array.copy;
    }
    
    #pragma mark - private
    + (GLStringArray *)gl_methodNamesWithClass:(Class)aClass{
        unsigned int m = 0;
        Method * methodList = class_copyMethodList(aClass, &m);
        GLStringMutableArray * array = [[GLStringMutableArray alloc] init];
        for (int i = 0; i < m; i ++) {
            Method method = methodList[i];
            SEL sel = method_getName(method);
            [array addObject:NSStringFromSelector(sel)];
        }
        return array.copy;
    }
    
    @end
    
  • 相关阅读:
    T-Sql 递归查询(给定节点查所有父节点、所有子节点的方法)
    禁用鼠标选中文字
    org.springframework.dao.InvalidDataAccessApiUsageException错误
    eclipse/myeclipse遇到的问题及解决方法
    WPF制作圆角窗体思路
    程序关闭后台进行
    转:Excel—“撤销工作表保护密码”的破解并获取原始密码
    C#压缩图片——高质量压缩方式
    【转载】.NET模拟POST登录并保持登录状态
    C#导出Excel,并设置简单格式
  • 原文地址:https://www.cnblogs.com/gulong/p/11598641.html
Copyright © 2011-2022 走看看